admin管理员组文章数量:1302978
I am having some difficulties building a javascript that gets all the images from a div ID, and add a link to the big image on each of the thumbnails. Here is my code.
<html>
<head>
<script>
function addGallery(){
var getDivId = document.getElementById("imgContainer");
var images = getDivId.getElementsByTagName("img").innerHTML;
for(var i=0; i<images.length; i++) {
getDivId.innerHtml = "<a id='g2Image' href='big/" + images[i].src + "'>" + images[i].src + "</a>";
}
}
</script>
</head>
<body onload='addGallery()'>
<div id="imgContainer">
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
</div>
</body>
</html>
Thank you in advance! Regards.
I am having some difficulties building a javascript that gets all the images from a div ID, and add a link to the big image on each of the thumbnails. Here is my code.
<html>
<head>
<script>
function addGallery(){
var getDivId = document.getElementById("imgContainer");
var images = getDivId.getElementsByTagName("img").innerHTML;
for(var i=0; i<images.length; i++) {
getDivId.innerHtml = "<a id='g2Image' href='big/" + images[i].src + "'>" + images[i].src + "</a>";
}
}
</script>
</head>
<body onload='addGallery()'>
<div id="imgContainer">
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
<img src="/images/galleries/img1.jpg" alt="" width="125" height="100" />
</div>
</body>
</html>
Thank you in advance! Regards.
Share Improve this question asked Jul 11, 2011 at 23:32 BrenoBreno 211 gold badge1 silver badge2 bronze badges 1- 2 Are you sure you want to do this in Javascript? When I have done things like this before, it's because I lack access to the server-side development environment. After reading through this, it seems like it might be cleaner to do this on the server side (if possible.) – Brian Stinar Commented Jul 11, 2011 at 23:39
3 Answers
Reset to default 2You are assigning innerHTML in each loop without concatenating the existing HTML.
Change:
getDivId.innerHTML = "<a id='g2Image' href='big/" + images[i].src + "'>" + images[i].src + "</a>";
to
getDivId.innerHTML += "<a id='g2Image' href='big/" + images[i].src + "'>" + images[i].src + "</a>";
Um... you're actually attempting to grab an object property from an array, which doesn't exist:
var images = getDivId.getElementsByTagName("img").innerHTML;
Should be changed to:
var images = getDivId.getElementsByTagName("img");
Edit 2
Note than in IE, the image.src
value will be the full path of the image so you can't just prepend 'big/' to them.
Edit
Oops, didn't see that you were adding A elements. Presumably you want the images to be inside the link. Instead of innerHTML, you can use DOM methods to creat the links:
function addGallery(){
var getDivId = document.getElementById("imgContainer");
var path = '/images/galleries/';
var images = toArray(getDivId.getElementsByTagName("img"));
var oA = document.createElement('a');
var a, image, parent;
for(var i=0, iLen=images.length; i<iLen; i++) {
image = images[i];
a = oA.cloneNode(false);
a.href = image.src.replace(path, '/big' + path);
image.parentNode.appendChild(a);
a.appendChild(image);
}
}
function toArray(a) {
var result = [];
var i = a.length;
while (i--) {
result[i] = a[i];
}
return result;
}
There is also a document.images collection that is all the images in the document, but likely you only want those in the div.
本文标签: javascriptGet all images from a div ID and add linksStack Overflow
版权声明:本文标题:javascript - Get all images from a div ID and add links - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741728239a2394723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论