如何使用jquery选择器来获取以下文本?

时间:2016-06-22 13:30:06

标签: jquery

假设网站的HTML看起来像这样。

<img src="pic/helloworld_123.jpg" width="48" border="0" alt="" title="">

如何使用jquery选择器获取以下文本?

helloworld_123.jpg

Thx

我测试了这个,但它没有用。

$("img[src*='']").text();

4 个答案:

答案 0 :(得分:2)

尝试:

$("img").attr("src").split("/").pop(); // it will get the src of image, split it using "/" and get the last index of an array which is "helloworld_123.jpg" 

&#13;
&#13;
console.log($("img").attr("src").split("/").pop());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<img src="pic/helloworld_123.jpg" width="48" border="0" alt="" title="">
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果我理解正确的话:

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

然后你可以根据需要解析src属性

$("img").attr('src').split('/').pop()

对于多张图片,请使用

$("img").each(function(){ $("img").attr('src'); }

答案 2 :(得分:0)

$(document).ready(function(){
  var src = $('img').attr('src');
  var name = src.split('/');
  alert(name[1]);          
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img src="pic/helloworld_123.jpg" width="48" border="0" alt="" title="">

答案 3 :(得分:-1)

$('img').attr('src').substring($('img').attr('src').indexOf('/') +1)
相关问题