如何隐藏包含特定关键字的表格行?
在阅读论坛时,我希望能够使用可自定义的关键字filter来隐藏某些行。
例如,在这个论坛上 ,我想隐藏某些用户名的任何行(第3列)。
难道只能在这个网站上写一个可以做到这一点的Greasemonkey脚本吗?
或者是否有一个Firefox插件可以做这种事情?
编写用户脚本以按关键字隐藏行并不难。
假设你有一个这样的表:
Post Title Author 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fred 2 Fred speaks Greek! Ethel 3 You keep using that function. I do not think it does what you think it does. Inigo Montoya
而且你想要隐藏包含Fred
行。
使用jQuery的强大function,您可以使用一行:
$(".filterMe tr:contains('Fred')").hide ();
如果您想将匹配限制为第3列(在本例中为作者),您可以使用:
$(".filterMe td:nth-of-type(3):contains('Fred')").parent ().hide ();
请注意:contains()
区分大小写。
在线演示:(显示并运行代码段。)
$("form").submit ( function (evt) { evt.preventDefault (); //-- Stop normal form submit. $(".filterMe tr").show (); //-- Reset row display: var filterTerm = $("#filterTxtInp").val (); var targJQ_Selector = ".filterMe td:nth-of-type(3):contains('" + filterTerm + "')"; //-- Hide the desired rows. $(targJQ_Selector).parent ().hide (); } );
table { border-collapse: collapse; } table, td, th { border: 1px solid gray; } td, th { padding: 0.3ex 1ex; text-align: left; }
Post Title Author 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fred 2 Fred speaks Greek! Ethel 3 You keep using that function. I do not think it does what you think it does. Inigo Montoya