2011-08-08 68 views
1

我有一個小問題,我想隱藏一些圖像。像gravatar和小圖片。有人可以給我一個正則表達式函數的例子嗎? 如何隱藏相似圖片:用jquery正則表達式過濾和隱藏圖像

<ul id="grid"><li><img id="photo" src="http://www.gravatar.com/avatar.php?gravatar_id%3D9698b3c319a46d14567b271cabcc85f1%26amp%3Brating%3DX%26amp%3Bsize%3D80%26amp%3Bdefault%3Dhttp%3A%2F%2Fwww.setupswarm.com%2Fwp-content%2Fplugins%2Fravatar%2Fcache%2F9698b3c319a46d145.png"></li></ul> 

回答

1

在這種情況下,沒有必要對正則表達式:

$('img[src^="http://www.gravatar.com/avatar.php"]').hide() 

(使用Attribute Starts With Selector)。對於更復雜的查詢,使用filter作爲參數。在這個功能中試圖弄清楚,如果圖像應該隱藏與否:

$('img').filter(function() { 
    if ($(this).attr('href').search(/http:\/\/www\.gravatar\.com/) > -1) { 
    return true; 
    } 
    return false; 
}); 
+0

我試過這個例子,但它不工作。我不知道爲什麼。也許有需要額外的jQuery庫? –

+0

呃..我的錯。 Tnx隊友,它工作得很好:) –