2012-12-30 123 views
0

我有一個變量,它存儲具有某些內容的錶行。我想使用%% LIKE %%方法搜索該字符串的特定術語,如果發現將該html行添加到僅包含已過濾行的新變量中。 如果可能,我希望搜索不區分大小寫。jQuery:在包含文本的字符串中查找標記

到目前爲止,我有以下的,這似乎不工作:

// current list 
var thisBlock = '<tr><td>Some content</td></tr><tr><td>Some other content</td></tr><tr><td>Even more content</td></tr>'; 

// new list 
var thisList; 

// phrase to be found 
var thisValue = 'Some'; 

// filter 
var thisItems = $(thisBlock).find("tr:contains('" + thisValue + "')"); 

// loop to append the found rows 
if (thisItems !== '' && typeof thisItems !== 'undefined') { 
    $.each(thisItems, function() { 
     thisList += $(this).clone().wrap('<div>').parent().html(); 
    }); 
} 

的問題似乎是與過濾器的一部分,我無法弄清楚如何做到這一點任何其他方式。

+1

正則表達式可能嗎? :) –

+0

可能,但我不熟悉正則表達式:( –

回答

1

試試這個:

var thisItems = $('<table>'+thisBlock+'</table>').find("tr:contains('" + thisValue + "')"); 

沒有包裹臺標籤我不認爲jQuery將看到它作爲一個有效的標籤,因爲它沒有根節點。

更新:下面的代碼正在努力將每個匹配的行添加到表#outTab,你可以使用它來達到你想要的位置嗎?

<table id="outTab"/> 

<script type="text/javascript"> 
// current list 
var thisBlock = '<table><tr><td>Some content</td></tr><tr><td>Some other content</td></tr><tr><td>Even more content</td></tr></table>'; 

// new list 
var thisList; 

// phrase to be found 
var thisValue = 'Some'; 

// filter 
var thisItems = $(thisBlock).find("tr:contains('" + thisValue + "')"); 

// loop to append the found rows 
if (thisItems !== '' && typeof thisItems !== 'undefined') { 
    $.each(thisItems, function(){ 
     console.log(this); 
     $('#outTab').append(this); 
    }); 
    console.log(thisItems); 
} 
</script> 
+0

謝謝,但我恐怕這似乎沒有任何區別 –

+0

你到底想要做什麼? – nfvindaloo

+0

結果將被顯示在表格的內部tbody標記 –

相關問題