[英]Remove all <tr> If does not contain “test” in text
I am making a chrome extension for the first time and need a little help with my Javascript. 我是第一次进行chrome扩展程序,需要一些Java脚本帮助。
In my popup menu I want a few buttons. 在弹出菜单中,我需要一些按钮。 Once someone presses this button lets say button "test".
一旦有人按下该按钮,就说按钮“测试”。 I want it to remove every single
<tr>
whom does not contain the word "test"
. 我希望它删除每个不包含单词
"test"
<tr>
。
I am making this because the filter functionality on this website I use a lot is very slow. 我这样做是因为我经常使用的该网站上的过滤器功能非常慢。 This way I can filter faster myself by removing the rows instead of the program searching through all of them.
这样,我可以删除行而不是通过程序搜索所有行来更快地进行过滤。
This is what I have so far: 这是我到目前为止的内容:
var searchString = 'TEST'; $("#tbody tr td:contains('" + searchString + "')").each(function Tester() { if ($(this).text() != searchString) { $(this).parent().remove(); } });
<p>Remove all rows which don't contain:</p> <button onclick="Tester()">TEST</button>
Firstly don't use inline JS. 首先,不要使用内联JS。 It's bad practice.
这是不好的做法。 Attach event handlers using unobtrusive JS instead.
而是使用不引人注意的JS附加事件处理程序。
To fix your actual issue, use the :contains
selector along remove()
, something like this: 要解决您的实际问题,请在
remove()
使用:contains
选择器,如下所示:
$('button').click(function() { var searchString = $(this).text(); $("#tbody tr td:contains('" + searchString + "')").closest('tr').remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Remove all rows which don't contain:</p> <button>TEST</button> <table> <tbody id="tbody"> <tr> <td>TEST</td> </tr> <tr> <td>Foo</td> </tr> <tr> <td>TEST</td> </tr> <tr> <td>Bar</td> </tr> </tbody> </table>
Try this 尝试这个
$("#tbody tr td").each( function () {
if ( $(this).text().indexOf( searchString ) == -1 ) { //notice the use of indexOf
$(this).parent().remove();//
}
});
Or you can check the row's text
itself 或者您可以检查行的
text
本身
$("#tbody tr").each( function () {
if ( $(this).text().indexOf( searchString ) == -1 ) {
$(this).remove();//
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.