javascript 在选择该图像时获取图像或图像 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7917342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:42:47  来源:igfitidea点击:

Get image or image url on select that image

javascriptimageonselectimageurl

提问by Manu

How can i get the image or image url on select that image using javascript, any examples?

如何使用 javascript 选择该图像时获取图像或图像 url,任何示例?

Thanks Manoj

谢谢马诺吉

回答by James Allardice

Your question is far from clear, but assuming an image with an idof myImage, and assuming that by "select" you mean "click", you can do this:

您的问题尚不清楚,但假设图像带有idof myImage,并假设“选择”的意思是“单击”,您可以这样做:

document.getElementById("myImage").onclick = function() {
    console.log(this.src);
}

Here's a working example. Clarify your question and I can clarify my answer.

这是一个工作示例。澄清你的问题,我可以澄清我的答案。

回答by arb

This is fairly straightforward using jQuery:

这使用 jQuery 相当简单:

$('#image_id').attr('src')

This will return the src url of the image with the id of "image_id".

这将返回 ID 为“image_id”的图像的 src url。

If you want this information on a click event, you can wire that up like this:

如果您想要有关单击事件的此信息,您可以像这样连接它:

$('#image_id').click(function(){
   alert($(this).attr('src')); 
});