Beispiel:JS-DOM-Tut-5.html

Aus SELFHTML-Wiki
Wechseln zu: Navigation, Suche
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" media="screen" href="./Beispiel:SELFHTML-Beispiel-Grundlayout.css">  
<script>
'use strict';
document.addEventListener('DOMContentLoaded', function () {
 
  let anzahl = 0,
      text ='';
 
  document.getElementById('button').addEventListener('click', gedrueckt);
   
  function gedrueckt() {
    anzahl = anzahl + 1; 

    if (anzahl < 10) {
      text = 'Sie haben den Button ' + anzahl +'mal gedrückt!';
    }
    else if (anzahl < 15) {
      text = 'Sie haben jetzt bereits ' + anzahl + 'mal geklickt.\n' +
             'Wollen Sie sich nicht einen Kaffee gönnen?';
    }
    else {
      text = 'Jetzt haben Sie schon ' + anzahl + 'mal geklickt - mir wird langweilig.';
    }
    document.querySelector('output').innerText = text;
  }
 
});
 
</script>  
<title>DOM-Tutorial-5</title>
</head>
 
<body>
  <h1>Auf Events reagieren - 1</h1>
 
  <button id="button">Drück mich!</button>
  <output></output>
 
</body>
</html>