[英]How to make .createElement("image") into a link as well
I've managed to get the images to list the way I want, now I'd each image to be a link, that points to it's own URL我已经设法让图像以我想要的方式列出,现在我将每个图像作为一个链接,指向它自己的 URL
What it is now:现在是什么:
<img src="01.jpg" />
What I want it to be:我希望它是什么:
<a href="01.jpg" target="_new"><img src="01.jpg" /></a>
Current Code:当前代码:
<script>
var fullLink = 'http://www.website.com/'; //Public URL
var ext = ".jpg"; //File Extension
for ( var i = 1; i < 6 ; i++ ) {
var link = document.createElement('a');
var elem = document.createElement("img");
if (i < 10) {
link.setAttribute("href", fullLink+"0"+i+ext);
elem.setAttribute("src", fullLink+"0"+i+ext);
} else {
link.setAttribute("href", fullLink+i+ext);
elem.setAttribute("src", fullLink+i+ext);
}
elem.setAttribute("height", "250px");
document.getElementById("images").appendChild(link);
document.getElementById("images").appendChild(elem);
}
</script>
<script>
var fullLink = 'http://www.website.com/'; //Public URL
var ext = ".jpg"; //File Extension
for ( var i = 1; i < 6 ; i++ ) {
var link = document.createElement('a');
var elem = document.createElement("img");
if (i < 10) {
link.setAttribute("href", fullLink+"0"+i+ext);
elem.setAttribute("src", fullLink+"0"+i+ext);
} else {
link.setAttribute("href", fullLink+i+ext);
elem.setAttribute("src", fullLink+i+ext);
}
elem.setAttribute("height", "250px");
link.appendChild(elem); //<----
document.getElementById("images").appendChild(link);
}
</script>
This should be what your're looking for.这应该是你要找的。 It puts the img you created as a child of the anchored link you created.
它将您创建的 img 作为您创建的锚定链接的子项。
No need to append the image to the images ID because it's contained in link
now.无需将图像附加到图像 ID,因为它现在包含在
link
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.