使用jQuery输出srcset的img.currentSrc [英] Output img.currentSrc of srcset using jQuery

查看:264
本文介绍了使用jQuery输出srcset的img.currentSrc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jQuery警报输出图像的当前来源:

I need to output the current source of an image using a jQuery alert:

alert($('.someclass img').attr('currentSrc'));

...但是警报输出未定义.

...however the alert is outputting undefined.

供参考,标记如下:

<div class="someclass">
    <img srcset="http://example.com/A.png, http://example.com/A@2x.png 2x">
</div>

推荐答案

currentSrc是属性,而不是属性.使用.prop('currentSrc')

currentSrc is a property not an attribute. Use .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输出srcset的img.currentSrc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆