Quelltext der Datei: http://www.michaelster.ch/jsTestFolder/appendTextChild.htm

Dateigrösse: 1.56 kb

[Anzeige ohne Zeilennummern]


 1 <!DOCTYPE html>
 2 <html lang="de">
 3 <head>
 4   <meta charset="utf-8">
 5   <title>DOM - appendChild</title>
 6 
 7 <script>
 8 // from: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
 9 // With ES2015 you can use this one:
10 Array.prototype.shuffle = function() {
11   let m = this.length, i;
12   while (m) {
13     i = (Math.random() * m--) >>> 0;
14     [this[m], this[i]] = [this[i], this[m]]
15   }
16   return this;
17 }
18 var a = [1, 2, 3, 4, 5, 6, 7];
19 var r = a.shuffle();
20 console.log(r);
21 </script>
22 
23 </head>
24  
25 <body>
26 
27 <h2>Headline</h2>
28 
29 
30 <button id="check">check var b</button>
31 <p id="ausgabe"></p>
32 
33 
34 <script>
35 
36 /*** http://xahlee.info/js/js_textContent_innerHTML_innerText_nodeValue.html ***/
37 /* the most compatible way to change a element's text content 
38    the script has to be after the requested element, here h2 */
39 var myElem = document.getElementsByTagName("h2")[0];
40 var newTxt = "Lovecats";
41 
42 (function(ele, text){
43     if ( ele.firstChild ) {
44         ele.firstChild.nodeValue = text;
45     } else {
46         ele.appendChild(document.createTextNode(text));
47     }
48 })( myElem, newTxt );
49 
50 var b;
51 var button = document.getElementById('check'); 
52     button.addEventListener('click', check);
53 var ausgabe = document.getElementById('ausgabe');
54 
55 function check(){
56   console.log('pop '+ b +' oop');
57   if(b) {
58     ausgabe.innerHTML = 'true: Variable b is: '+b;
59   } else {
60     ausgabe.innerHTML = 'false: Variable b is: '+!!b; // ohne !! is undefined
61   }
62   b = b ? false : true;
63 }
64 </script>
65 
66 </body>
67 </html>
68