2

次のようなSTRING(ドキュメントにはありません)があります

<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender-    samsungativ_thumbnail.jpg"> Freedom is a wonderful thing and the folks at BearExtender want Microsoft users to enjoy more of it. Its new 1,200mW USB WiFi booster for PCs finally caught up with the Mac version, which extends WiFi range up to four times more than usual. One lucky reader will get to savor this new found freedom ...<div>

文字列の最初の画像のソースを取り除きたい。次を使用してjqueryを使用して実行できることを知っています: String.find('img').first().attr('src');

jqueryなしで同じことを達成するにはどうすればよいですか?

4

3 に答える 3

3

単純な JS テストをいくつか実行して、そこから src を次のように取り除きます。

var str = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender-    samsungativ_thumbnail.jpg"> Freedom is a wonderful thing and the folks at BearExtender want Microsoft users to enjoy more of it. Its new 1,200mW USB WiFi booster for PCs finally caught up with the Mac version, which extends WiFi range up to four times more than usual. One lucky reader will get to savor this new found freedom ...<div>'

var imgExists = str.indexOf('<img src="');

if (imgExists > -1) {
    var i = imgExists + 10;

    str = str.substr(i);
    str = str.substr(0, str.indexOf('"'));
    alert(str); 
     // returns = http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender-    samsungativ_thumbnail.jpg 

    // to remove all the spaces you might also want to do another replace after the last 2 substr's
    // str = str.replace(/\s/g, ''); 
}

jsFiddle デモ

于 2013-10-29T14:57:09.743 に答える
1

次の方法で取得できます。

var i = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender-    samsungativ_thumbnail.jpg"> Freedom is a wonderful thing and the folks at BearExtender want Microsoft users to enjoy more of it. Its new 1,200mW USB WiFi booster for PCs finally caught up with the Mac version, which extends WiFi range up to four times more than usual. One lucky reader will get to savor this new found freedom ...<div>';
    alert(i.substring(i.indexOf('http'), i.lastIndexOf('"')));
于 2013-10-29T14:57:08.407 に答える