简体   繁体   English

获取图像的来源并在单击时在新窗口中打开

[英]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. 我在表格中有一系列图像,当我们点击图像时,我需要使用JavaScript或Jquery在新窗口/标签中打开它们。 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. 由于这些图像可能会随着时间而改变,我认为获取这些图像的src属性并在新窗口中自动使用该src属性打开它们是个好主意。 that way we can reduce some maintenance for the code. 这样我们就可以减少对代码的一些维护。

Since I am a newbie for JavaScript. 因为我是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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