zıə*_*əʇs 63 jquery contains css-selectors jquery-selectors
假设我有:
<li id="1">Mary</li>
<li id="2">John, Mary, Dave</li>
<li id="3">John, Dave, Mary</li>
<li id="4">John</li>
Run Code Online (Sandbox Code Playgroud)
如果我需要找到包含"John"和"Mary"的所有<li>元素,我将如何构建jQuery?
搜索单个字符串似乎很容易:
$('li:contains("John")').text()
Run Code Online (Sandbox Code Playgroud)
我正在寻找类似下面的伪代码:
$('li:contains("John")' && 'li:contains("Mary")').text()
Run Code Online (Sandbox Code Playgroud)
谢谢!
Ada*_*iss 114
找到li包含两个玛丽和约翰的文本:
$('li:contains("Mary"):contains("John")')
Run Code Online (Sandbox Code Playgroud)
要查找li包含EITHER Mary 或 John的文本:
$('li:contains("Mary"), li:contains("John")')
Run Code Online (Sandbox Code Playgroud)
只要想到:contains它就像是一个类声明,如.class:
$('li.one.two'). // Find <li>'s with classes of BOTH one AND two
$('li.one, li.two'). // Find <li>'s with a class of EITHER one OR two
Run Code Online (Sandbox Code Playgroud)
它与以下相同:contains:
$('li:contains("Mary"):contains("John")'). // Both Mary AND John
$('li:contains("Mary"), li:contains("John")'). // Either Mary OR John
Run Code Online (Sandbox Code Playgroud)
小智 5
正确的语法是 $("li:contains('John'),li:contains('Mary')").css("color","red")
但是我发现,如果要测试的案例很多,jQuery的性能将非常糟糕(尤其是在IE6上,我知道它已经很旧了,但仍在使用中)。因此,我决定编写自己的属性过滤器。
这是使用方法: $("li:mcontains('John','Mary')").css("color","red")
jQuery.expr[':'].mcontains = function(obj, index, meta, stack){
result = false;
theList = meta[3].split("','");
var contents = (obj.textContent || obj.innerText || jQuery(obj).text() || '')
for (x=0;x<theList.length;x++) {
if (contents.indexOf(theList[x]) >= 0) {
return true;
}
}
return false;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54103 次 |
| 最近记录: |