简体   繁体   中英

Get the Source of an image and open in a new window when clicked

I have an series of images in a table and I need to open them in new window/tab using JavaScript or Jquery when we click on the image. Since these images might change over time, I thought it would be a good idea to get the src attribute of these images and open them in new window using that src attribute automatically. that way we can reduce some maintenance for the code.

Since I am a newbie for JavaScript. I need help in figuring out how to achieve this.

How about:

$("table tr td img").click(function() {
    var src = $(this).attr("src");
    window.open(
        '/' + src, //change this to your relative path + src
        '_blank' // <- This is what makes it open in a new window.
    );
});

使用target="_blank"作为锚标记

<a href="your-image-path" target="_blank"><img src="your-image-path"/></a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM