“JavaScript -Schleifen durch Klassenelemente” Code-Antworten

JavaScript -Schleifen durch Klassenelemente

var els = document.getElementsByClassName("myClass");
for(var i = 0; i < els.length; i++)
{
  console.log(els[i]);
}
Friendly Hawk

JavaScript -Schleife über der Klasse

var myElements = document.getElementsByClassName("some_class_name");
for(var i = 0; i < myElements.length; i++){
	console.log(myElements[i]);
}
Grepper

JavaScript -Schleife über der Klasse

var person={
 	first_name:"johnny",
  	last_name: "johnson",
	phone:"703-3424-1111"
};
for (var property in person) {
  	console.log(property,":",person[property]);
}
Grepper

JavaScript -Schleifen durch Klassenelemente

// get elements based on their class name/s
const elements = Array.from(document.getElementsByClassName("css_class_name"));
// using for loop
for (let i = 0; i < elements.length; i++) {
	console.log(elements[i]);
}
// using for of loop
for (const element of elements) {
	console.log(element);
}
/*
using for in loop (Note: this will iterate through keys
in an object, otherwise you wouldn't need to convert the
HTMLCollection returned by
document.getElementsByClassName into an Array)
*/
for (const i in elements) {
	console.log(elements[i]);
}
/*
Note: these all achieve the same thing, but are slightly
different. The basic for loop is actually faster than
the for of and for in loops (by a miniscule, unnoticable
amount). Generally, you want to use whichever version
is most readable for you personally. In this case, the
for of loop is probably the most readable, followed by
the for loop. It is incredibly unlikely you would ever
need use a for in loop in a scenario like this.
*/
MattDESTROYER

Ähnliche Antworten wie “JavaScript -Schleifen durch Klassenelemente”

Fragen ähnlich wie “JavaScript -Schleifen durch Klassenelemente”

Weitere verwandte Antworten zu “JavaScript -Schleifen durch Klassenelemente” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen