1

HTMLを含む文字列からpngリンクを取得しようとしています。

文字列は次のようになります。

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>';

私はjQueryコードを介して画像リンクを取得しようとしています:

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

しかし、上記の例では、SCRIPT5022が返されます。構文エラー、認識されない式:

私は何が欠けているでしょうか?

4

1 に答える 1

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の例

于 2013-02-15T20:48:00.977 に答える