JavaScript: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „Kategorie:Sprache = DOM-Elemente finden = <pre>var list = document.getElementsByTagName("UL")[0]; var elem = document.getElementById("xyz"); </pre> = Ein…“)
 
Zeile 27: Zeile 27:
== Dictionary ==
== Dictionary ==
<pre>var x = { "zahl" : 33, "pair" : { "x" : true } };
<pre>var x = { "zahl" : 33, "pair" : { "x" : true } };
</pre>
= Klassen =
<pre>'use strict';
class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
  sayName() {
    ChromeSamples.log('Hi, I am a ', this.name + '.');
  }
  sayHistory() {
    ChromeSamples.log('"Polygon" is derived from the Greek polus (many) ' +
      'and gonia (angle).');
  }
}
// Classes are used just like ES5 constructor functions:
let p = new Polygon(300, 400);
p.sayName();
</pre>
</pre>

Version vom 10. Juni 2017, 21:55 Uhr

DOM-Elemente finden

var list = document.getElementsByTagName("UL")[0];
var elem = document.getElementById("xyz");

Einbindung in HTML

 <button onclick="myFunction()">Click me</button> 
<p onclick="myFunction()">Click me to change my text color.</p>
<script>
function myFunction() {
    document.getElementById("demo").style.color = "red";
}
<button onclick="myFunction()">Copy Text</button>

<script>
function myFunction() {
    document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

Typen

Dictionary

var x = { "zahl" : 33, "pair" : { "x" : true } };

Klassen

'use strict';
class Polygon {
   constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
  sayName() {
    ChromeSamples.log('Hi, I am a ', this.name + '.');
  }
  sayHistory() {
    ChromeSamples.log('"Polygon" is derived from the Greek polus (many) ' +
      'and gonia (angle).');
  }
}
// Classes are used just like ES5 constructor functions:
let p = new Polygon(300, 400);
p.sayName();