2012-12-30 48 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标记 –

相关问题