:contains() 应该如何使用?
我是 jQuery 新手,但只是尝试创建一个可用于过滤表的函数。我已经设置了表格,以便我可以按类选择所有行(效果很好)并在结果上调用each()。在每个()的回调中,我有这个 if 语句:
if ($(this).find("td[name=firstName]:contains('ke')").size() > 0)
但是,当我知道它在那里时,它找不到匹配项。如果我取出 contains() 调用,它确实会找到表格单元格,但是我真的不确定出了什么问题。不幸的是(对我来说)这可能就像使用错误的语法一样简单...:(
最终结果是为选择器字符串提供一个变量,但因为即使直接使用字符串也不起作用...
I'm new to jQuery, but just attempting to create a function I can use to filter a table. I've got the table set up so that I can select all the rows by the class (which works fine) and call each() on the result. Inside the callback to each() I've got this if statement:
if ($(this).find("td[name=firstName]:contains('ke')").size() > 0)
However it doesn't find a match when I know it's there. If I take out the contains() call it does find the table cell, however so I'm really not sure what's wrong. Unfortunately (for me) this could be as simple as using the wrong syntax... :(
The end result would be to have a variable for the selector string, but since even using the string directly isn't working...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样扩展 jQuery 的选择器:
这将为您提供一个新的伪选择器
:icontains(text)
,其工作方式类似于:contains(text)
,但不区分大小写。请参阅:
:contains(text)
注释部分 >You could extend jQuery's selectors with this:
That would give you a new pseudo-selector
:icontains(text)
that works like:contains(text)
, but case-insensitive.See: Comment Section on jQuery's documentation page for
:contains(text)
将
.size()
替换为.length
并检查是否找到小写“ke”
replace
.size()
with.length
also check that it is finding lowercase 'ke'
这对我有用(http://jsfiddle.net/dactivo/PHMkx/)。
它查找属性为“name”、值为“FirstName”的td,并且在td内部,也就是说,td内部的html包含字符串“prueba”。 size() 或 length 都可以。
This is working for me (http://jsfiddle.net/dactivo/PHMkx/).
It looks for a td with an attribute "name" with value "FirstName" and inside the td, that's to say, the html inside td, contains the string "prueba". size() or length here both work.