admin管理员组文章数量:1434909
From my understanding, document.querySelector
returns a Node
object. I can then call appendChild
on this object.
I execute the following code to append a bunch of divs to my container div:
var container = document.querySelector('.container');
for (var i = 0; i < 400; i++) {
var block = document.createElement('div');
block.className = 'block';
container.appendChild(block);
}
And end up with the following structure:
<div class="container">
<div class="block"></div>
<div class="block"></div>
...
<div class="block"></div>
</div>
How can I loop through each element in my container div and add a new class to it using my existing container
variable?
I have tried this:
...
container.childNodes[i].className = 'myClass';
It seems I need to access the Element
object of the child Node
, but I'm not sure how to do this.
From my understanding, document.querySelector
returns a Node
object. I can then call appendChild
on this object.
I execute the following code to append a bunch of divs to my container div:
var container = document.querySelector('.container');
for (var i = 0; i < 400; i++) {
var block = document.createElement('div');
block.className = 'block';
container.appendChild(block);
}
And end up with the following structure:
<div class="container">
<div class="block"></div>
<div class="block"></div>
...
<div class="block"></div>
</div>
How can I loop through each element in my container div and add a new class to it using my existing container
variable?
I have tried this:
...
container.childNodes[i].className = 'myClass';
It seems I need to access the Element
object of the child Node
, but I'm not sure how to do this.
-
1
That should work. What is
i
? Are all of the containerschildnodes
elements? Trychildren
instead. – Bergi Commented Nov 24, 2015 at 1:15 - 2 difference between node and element – Barmar Commented Nov 24, 2015 at 1:19
-
The nodes returned by
querySelector
are all elements, because you can't write a selector for non-element nodes. – Barmar Commented Nov 24, 2015 at 1:20 - 1 Why don't you add the class in the loop where you create the DIVs? – Barmar Commented Nov 24, 2015 at 1:21
-
1
@cornflakes24: I don't understand, isn't that log expected? What doesn't work?
.children
is a property of theElement
subclass ofNode
– Bergi Commented Nov 24, 2015 at 1:38
2 Answers
Reset to default 0Can you not just add it when you create the divs ?
var container = document.querySelector('.container');
for (var i = 0; i < 400; i++) {
var block = document.createElement('div');
block.className = 'block myClass';
container.appendChild(block);
}
To add classes to the elements in the container
variable, I used the following code:
container.children[i].className = 'myClass';
I had to use children
instead of childNodes
. You can see the context in which this code was used here: http://codepen.io/robkom/pen/RWmodz.
本文标签: javascriptHow to get Element from NodeStack Overflow
版权声明:本文标题:javascript - How to get Element from Node - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745636128a2667568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论