我需要使用 jQuery 警报输出图像的当前源:
alert($('.someclass img').attr('currentSrc'));
...但是警报输出未定义。
仅供引用,标记如下:
<div class="someclass">
<img srcset="http://example.com/A.png, http://example.com/A@2x.png 2x">
</div>
最佳答案
currentSrc
是属性而不是属性。使用 .prop('currentSrc')
$(function() {
console.log($("img").prop("currentSrc"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="someclass">
<img srcset="https://webkit.org/demos/srcset/image-1x.png, https://webkit.org/demos/srcset/image-2x.png">
</div>
关于jquery - 使用jQuery输出srcset的img.currentSrc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36684814/