admin管理员组文章数量:1405292
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
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 = '/'; //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>
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
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./'; //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>
Share
Improve this question
edited Aug 15, 2019 at 5:55
Arnaud
7,44910 gold badges52 silver badges72 bronze badges
asked Aug 20, 2014 at 12:16
user2077592user2077592
2
- 1 Who the heck downvoted this question? This question shows reasonable effort, an attempted solution, the expected output and the current output. It's well formatted by site standards. Not every knows everything about javascript or enough to search for when something goes wrong. – KyleMit ♦ Commented Aug 20, 2014 at 12:32
-
See this fiddle for a solution and a couple other modifications. You shouldn't do the lookup for
document.getElementById("images")
on every loop. Instead, do it once before thefor
and save it as a variable. There's also easier ways to pad the image number to reduce the cyclomatic plexity and increase the readability. – KyleMit ♦ Commented Aug 20, 2014 at 12:38
1 Answer
Reset to default 6<script>
var fullLink = 'http://www.website./'; //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.
No need to append the image to the images ID because it's contained in link
now.
本文标签: javascriptHow to make createElement(quotimagequot) into a link as wellStack Overflow
版权声明:本文标题:javascript - How to make .createElement("image") into a link as well - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744235962a2596554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论