如何获取 jquery 中 srcset 属性的最后一个值?

Moh*_*hNj 0 html jquery

我的代码是:

<figure class="im-entry-thumb clearfix">
    <img width="770" height="480" src="http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-770x480.jpg" class="attachment-post size-post wp-post-image" alt="" srcset="http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-770x480.jpg 770w, http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-280x175.jpg 280w" sizes="(max-width: 770px) 100vw, 770px">  </figure>
Run Code Online (Sandbox Code Playgroud)

我想获取我的最后一个值 srcset。方法:

http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-280x175.jpg
Run Code Online (Sandbox Code Playgroud)

请帮忙。坦克

JYo*_*ThI 5

像这样使用split()和pop()

//alert($('img').attr('srcset').split(',').pop());

console.log($('img').attr('srcset').split(',').pop());

console.log($('img').attr('srcset').split(',').pop().trim().split(' ')[0]);
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<figure class="im-entry-thumb clearfix">
    <img width="770" height="480" src="http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-770x480.jpg" class="attachment-post size-post wp-post-image" alt="" srcset="http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-770x480.jpg 770w, http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-280x175.jpg 280w" sizes="(max-width: 770px) 100vw, 770px">  </figure>
Run Code Online (Sandbox Code Playgroud)