javascript 带有 jQ​​uery 的图像绝对源路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6181366/
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-25 19:47:49  来源:igfitidea点击:

Image Absolute source path with jQuery

javascriptjqueryhtmlcssimage

提问by James

This code gives something like ../movies/1.png, the exact src attribute:

这段代码给出了类似的内容../movies/1.png,确切的 src 属性:

$("img").attr("src");

How do I get absolute path of an image with jQuery?

如何使用 jQuery 获取图像的绝对路径?

I want to get something like http://site.com/movies/1.png, not ../movies/1.png

我想得到类似的东西http://site.com/movies/1.png,而不是../movies/1.png

回答by jAndy

This can easily be done by accessing the .srcproperty directly on the <img>:

这可以通过.src直接在 上访问属性来轻松完成<img>

$('img')[0].src;

example: http://jsfiddle.net/9mKz2/

示例http: //jsfiddle.net/9mKz2/

(I didn't test it on all browser right now)

我现在没有在所有浏览器上测试它