2013-02-15 5 views
1

안녕하세요. 모든 HTML이 들어있는 문자열에서 PNG 링크를 가져 오려고합니다.jQuery는 문자열에서 이미지 링크를 가져옵니다.

문자열은 다음과 같습니다

var newString = $('a[href$=".png"]', thecode).attr('href'); 
console.log(newString); 

그러나 SCRIPT5022 다시 반환 위의 예 : 구문 오류, 인식 할 수없는 나는 jQuery 코드를 통해 이미지 링크를 얻기 위해 노력 해왔다

var thecode = '[caption id="attachment_1794" align="alignleft" width="210"] 
    <a href="http://www.website.com/wp-content/uploads/2012/10/Screen-Shot-2012-10-30-at- 
    8.52.48-AM.png"><img class="size-medium wp-image-1794" title="Difference between and 
    affiliate programs" src="http://www.website.com/wp-content/uploads/2012/10/Screen- 
    Shot-2012-10-30-at-8.52.48-AM-210x300.png" alt="Learn the and affiliate programs" 
    width="210" height="300" /></a> Learn the and affiliate programs[/caption]<p><span 
    style="font-size: small;">An article by CEO of AD, Inc. (AD®), has been published by 
    <a title="Learn the and affiliate programs" href="http://www.mmag.com/articles/85281" 
    target="_blank">M Magazine</a>. The article is titled: "Putting The Right Place." </span>'; 

표현식 :

무엇이 실종 되셨습니까?

+0

이 오류, 당신은 브라우저를 사용하는이 없어? –

+0

IE 버전 9를 사용 중입니다. – StealthRT

+0

PDF 또는 PNG를 가져 오시겠습니까? – j08691

답변

6

이 시도 :

var thecode = '[caption id="attachment_1794" align="alignleft" width="210"] <a href="http://www.website.com/wp-content/uploads/2012/10/Screen-Shot-2012-10-30-at-8.52.48-AM.png"><img class="size-medium wp-image-1794" title="Difference between and affiliate programs" src="http://www.website.com/wp-content/uploads/2012/10/Screen-Shot-2012-10-30-at-8.52.48-AM-210x300.png" alt="Learn the and affiliate programs" width="210" height="300" /></a> Learn the and affiliate programs[/caption]<p><span style="font-size: small;">An article by CEO of AD, Inc. (AD®), has been published by <a title="Learn the and affiliate programs" href="http://www.mmag.com/articles/85281" target="_blank">M Magazine</a>. The article is titled: "Putting The Right Place." </span>'; 

var newString = $('<div>'+ thecode + '</div>'); 
console.log($('a[href$=".png"]',newString).attr('href')); 

jsFiddle example

+0

AH, 이유는 ... ** [** 및 **] **입니다. 감사합니다 j08691! – StealthRT

+0

왜 문자열을'div'에 래핑해야합니까? – JSuar

+1

@JSuar jquery가 html을 파싱하는 방식 때문에 div에 래핑되어야합니다. 그렇지 않으면 모든 요소를 ​​가져올 수 없습니다. jquery는 최상위 수준의 텍스트 노드를 잘 처리하지 못합니다. –

관련 문제