Javascript 使用 jQuery 获取图像 src
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6952173/
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
Get image src with jQuery
提问by Jasper
<img src="../img/arnold.png" alt="Arnold">
How do I get with jQuery absolute path of this image?
如何获得此图像的 jQuery 绝对路径?
img.attr("src")
gives me just "../img/arnold.png", should give something like "http://site.com/data/2011/img/arnold.png" (full url).
img.attr("src")
只给我“ ../img/arnold.png”,应该给出类似“ http://site.com/data/2011/img/arnold.png”(完整网址)的内容。
回答by Reigel
回答by Bradley Staples
I don't know that you can get it with jQuery, but you can get it with just the native JavaScript image object.
我不知道您是否可以使用 jQuery 来获取它,但是您可以只使用本机 JavaScript 图像对象来获取它。
var getSrc = function(imgSource) {
var img = new Image();
img.src = imgSource;
return img.src;
};
Just call it with x = getSrc(srcAttribute)
or something similar where your parameter is the string or literal holding the src you currently have in your html/image. It will return something like http://your/site/path/to/image.jpg
只需使用x = getSrc(srcAttribute)
或类似的方式调用它,其中您的参数是保存当前在 html/image 中的 src 的字符串或文字。它会返回类似http://your/site/path/to/image.jpg 的内容
回答by Samir Karmacharya
Give current clicked image source in jQuery
在 jQuery 中给出当前点击的图像源
jQuery(document).ready(function($){
$('body').on('click','img',function(){
alert('it works');
var imgsrc=$(this).attr('src');
alert(imgsrc);
});
});