javascript - How to get Element from Node - Stack Overflow

From my understanding, document.querySelector returns a Node object. I can then call appendChild on thi

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.

Share Improve this question edited Nov 24, 2015 at 2:11 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Nov 24, 2015 at 1:10 chipit24chipit24 6,99711 gold badges52 silver badges75 bronze badges 9
  • 1 That should work. What is i? Are all of the containers childnodes elements? Try children 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? .childrenis a property of the Element subclass of Node – Bergi Commented Nov 24, 2015 at 1:38
 |  Show 4 more ments

2 Answers 2

Reset to default 0

Can 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636129a4637403.html

相关推荐

  • javascript - How to get Element from Node - Stack Overflow

    From my understanding, document.querySelector returns a Node object. I can then call appendChild on thi

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信