简体   繁体   English

如何使用jQuery获取img url?

[英]How to get an img url using jQuery?

Is it possible to get the actual URL (as opposed to the src attribute value) of an image within the current DOM using jQuery or JavaScript? 是否可以使用jQuery或JavaScript获取当前DOM中图像的实际URL(而不是src属性值)?

ie retrieve "example.com/foo.jpg" as opposed to "foo.jpg" (taking <base> elements into account) 即检索“example.com/foo.jpg”而不是“foo.jpg”(考虑<base>元素)

What about any other interesting properties such as the mime type, file size or, best of all, the actual binary data? 那么任何其他有趣的属性,如mime类型,文件大小,或者最重要的是,实际的二进制数据呢?

I wonder if jQuery is using .getAttribute() - using .src always seems to give the absolute URL: 我想知道jQuery是否正在使用.getAttribute() - 使用.src似乎总是给出绝对URL:

<img src="foo.jpg" id="i">

<script>
var img = document.getElementById('i');

alert(img.getAttribute('src')); // foo.jpg
alert(img.src);                 // http://..../foo.jpg
</script>

To get the rest of the information, could you use an AJAX request and look at the header & data sent? 要获取其余信息,您是否可以使用AJAX请求并查看发送的标头和数据?

$(you IMG selector).attr('src'); $(你的IMG选择器).attr('src');

for instance, if we have an <img src='bla-bla.jpg' id='my_img'> 例如,如果我们有<img src='bla-bla.jpg' id='my_img'>

then we'll do: 然后我们会做:

$('#my_img').attr('src');

simple 简单

$("#my_img").src;

just it resolve the problem 只是它解决了这个问题

var img = $('img');
alert(img.attr('src')); // foo.jpg
var fullpath = window.location.host + img.attr('src');
alert(fullpath); // http://example.com/foo.jpg

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

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