admin管理员组文章数量:1289542
I'm attempting to set the IDs of a group of divs "galleryboxes" (that are inside a another div, "galleryimages") to be increasing increments, ie. picture1, picture2, etc.
I've seen questions similar to this and tried to get my head around them, but it's difficult. Right now my questions are, what do I get from the second line here? What is 'galleryboxes'? Am I able to cycle through it like an array and label ids like I'm attempting to?
var galleryimages = document.getElementById("galleryimages");
var galleryboxes = galleryimages.getElementsByTagName("div");
var k = 1;
for (var i = 0; i < galleryboxes.length; i++) {
galleryboxes[i].setAttribute=("id", "picture" + k);
k++;
}
The code seems to work, but I don't think I'm actually labeling the divs I want to be labeling; I check what the contents are of "picture1" and it es up as null (It should be full of a picture and some text).
I'm attempting to set the IDs of a group of divs "galleryboxes" (that are inside a another div, "galleryimages") to be increasing increments, ie. picture1, picture2, etc.
I've seen questions similar to this and tried to get my head around them, but it's difficult. Right now my questions are, what do I get from the second line here? What is 'galleryboxes'? Am I able to cycle through it like an array and label ids like I'm attempting to?
var galleryimages = document.getElementById("galleryimages");
var galleryboxes = galleryimages.getElementsByTagName("div");
var k = 1;
for (var i = 0; i < galleryboxes.length; i++) {
galleryboxes[i].setAttribute=("id", "picture" + k);
k++;
}
The code seems to work, but I don't think I'm actually labeling the divs I want to be labeling; I check what the contents are of "picture1" and it es up as null (It should be full of a picture and some text).
Share Improve this question edited Jul 7, 2015 at 14:16 MattTreichel asked Dec 22, 2012 at 18:57 MattTreichelMattTreichel 1,5614 gold badges21 silver badges37 bronze badges 05 Answers
Reset to default 5What do I get from the second line here?
The line in question is this:
var galleryboxes = galleryimages.getElementsByTagName("div");
What this does is provide a live nodeList
plete with all the elements with the tag name <div>
that are child to the element which the variable galleryimages
represents. Note that this is not an actual array, and there are distinguishing characteristics between an array and a nodeList
. Most notably, a nodeList
does not inherit from the Array
constructor, and thus does not contain essential methods like push
, pop
, and sort
mon to all arrays.
Am I able to cycle through it like an array and label ids like I'm attempting to?
Yes. This is made easy for us because fortunately nodeList
s have a length
property. You've already showed how you can loop through them, but the line on which you set the attribute is ill-formed:
galleryboxes[i].setAttribute=("id", "picture" + k);
The equal sign should not be there. This invokes a syntax error. If you take the equal out, the code works fine.
galleryboxes
give you a node list which you can iterate through like an array but you have an error here
galleryboxes[i].setAttribute=("id", "picture" + k);
should be
galleryboxes[i].setAttribute("id", "picture" + k);
notice the lack of =
It can also be done using the id property.
galleryboxes[i].id = "picture" + k;
galleryboxes is an array that contains all the divs from within the element "galleryimages"
<html>
<head>
...
<head>
<body>
<div id="galleryimages">
<div class="img_container"></div>
<p>This text will not be in the array...</p>
<div class="img_container"></div>
<div class="img_container"></div>
</div>
</body>
</html>
If your code looked something like above, the variable galleryboxes
would only contain the divs.
As for when you check the contents of "picture1" it is probably null, I think you have defined an background image for #picture1 in css, am I right?
Anyways what you should do is too use something like firebug to see the generated DOM. That way you can debug much easier.
You will need to set the attribute directly (galleryboxes[i].id
) if you want to access it programatically afterwards (at least if you want it to work consistently). So:
var galleryimages = document.getElementById("galleryimages");
var galleryboxes = galleryimages.getElementsByTagName("div");
for (var i = 0; i < galleryboxes.length; i++) {
galleryboxes[i].id = "picture" + (i + 1);
}
See this jsfiddle for an example.
I know, you are looking for javascript answers here but you can also try this jQuery code, if you want too:
$("#galleryimages > div").each(function(i) {
$(this).prop("id", "picture" + (i + 1));
});
See just 3 lines of code :)
本文标签: galleryJavascript getElementsByTagName(quotdivquot) what am I labelingStack Overflow
版权声明:本文标题:gallery - Javascript getElementsByTagName("div") what am I labeling? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741463153a2380155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论