javascript - How to make .createElement("image") into a link as well - Stack Overflow

I've managed to get the images to list the way I want, now I'd each image to be a link, that

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 the for 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
Add a ment  | 

1 Answer 1

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.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信