2016-06-22 2 views
0

웹 사이트의 HTML이 이렇게 보인다고 가정합니다.jquery selector를 사용하여 다음 텍스트를 얻는 방법?

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

다음 텍스트를 가져 오는 데 jquery selector를 사용하는 방법은 무엇입니까?

helloworld_123.jpg 

들으 나는이 테스트했지만 작동하지 않습니다.

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

사용하십시오 src 속성을 구문 분석 할 수 있습니까? –

+0

텍스트 만 해당 없음 –

+0

* "텍스트 만"* 이미지에는 "텍스트"가 없습니다. –

답변

-1
$('img').attr('src').substring($('img').attr('src').indexOf('/') +1) 
+2

자세한 내용을 수정하십시오. 코드 전용 및 "시도하십시오"답변은 검색 가능한 콘텐츠가 없으므로 권장하지 않으며 누군가가 "시도해"야하는 이유를 설명하지 않습니다. 우리는 여기서 지식을위한 자원이되기 위해 노력합니다. – abarisone

+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" 

console.log($("img").attr("src").split("/").pop());
<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="">

1

나는 바로 그것을 이해한다면 :

$("img").attr('src'); 
당신이

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

을 원하는

그럼 당신은 여러 개의 이미지는 이미지의 URL을 얻을하려고

$("img").each(function(){ $("img").attr('src'); } 
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="">

관련 문제