【问题标题】:jQuery : find tags within the string which contain textjQuery:在包含文本的字符串中查找标签
【发布时间】:2012-12-30 15:58:40
【问题描述】:

我有一个变量,它存储带有一些内容的表格行。我想使用 %%LIKE%% 方法在该字符串中搜索特定术语,如果找到,则将此 html 行添加到仅包含过滤行的新变量中。 如果可能,我希望搜索不区分大小写。

到目前为止,我有以下内容,但似乎不起作用:

// 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();
    });
}

问题似乎出在过滤器部分,我无法弄清楚如何以其他方式解决。

【问题讨论】:

  • 可能,但我对正则表达式不太熟悉:(

标签: jquery regex search filter


【解决方案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>

【讨论】:

  • 谢谢,但恐怕这似乎没有任何区别。
  • 你到底想对结果做什么?
  • 结果显示在table的tbody标签内
  • 我需要替换表格的内容,而不是一一追加元素。
  • 它只适用于在块周围添加表格标签 - 它在我的代码中有所不同,不允许执行代码并且它在此代码之外。谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2023-01-31
  • 2010-11-30
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多