+ 1
How to order html elements with JS
Ok, let’s say I got 3 elements in the HTML - all with the same class and also different class: Class = “a one”; Class = “a two”; Class = “a three”; CSS: .a{ position: absolute; } Now if I wanted to order them by Z-index or transformZ(#px), I could use nth-child(), but imagine if I had 100 of those elements - that would take a long time to do. So how would I do it in JS? I’m thinking we’d start like this: var elms = doc...selectorAll(“.a”); var i = 0; Then i++ or i+= #; I don’t know how to finish that
3 ответов
0
then loop trough the collection with for of loop
for element of elms{
//work here
}
0
Taste well, ya loops.
but what would the actual code look like?
-
ok, we start with getting the elements...
var elms = document.querySelectorAll(“.a”);
var i = 0;
...
elms.style.zIndex = i++;
..
0
pretty close yeah