jQuery: zawiera selektor do wyszukiwania wielu ciągów
Zakładając, że mam:
<li id="1">Mary</li>
<li id="2">John, Mary, Dave</li>
<li id="3">John, Dave, Mary</li>
<li id="4">John</li>
Jeśli muszę znaleźć wszystkie elementy
Wyszukiwanie pojedynczego ciągu wydaje się łatwe:
$('li:contains("John")').text()
Szukam czegoś takiego jak następujący pseudo kod:
$('li:contains("John")' && 'li:contains("Mary")').text()
Dzięki! 52
Author: zıəs uɐɟəʇs, 2010-03-10
4 answers
Odpowiedź
Jest tak:
$('li:contains("Mary"):contains("John")')
Wyjaśnienie
Pomyśl o :contains
jakby to była deklaracja klasy, jak .class
:
$('li.one, li.two'). //you want either one OR two
$('li.one.two'). //you want BOTH on one element
// same with :contains
$('li:contains("mary"), li:contains("john")'). // either mary OR john
$('li:contains("mary"):contains("john")'). //li, which contains both
Demo
94
Author: Adam Kiss,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-09 14:25:06
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-09 14:25:06
A może
$('li:contains("John"),li:contains("Mary")')
7
Author: Lazarus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-10 12:35:11
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-10 12:35:11
Odpowiedź
Poprawna składnia to $("li:contains('John'),li:contains('Mary')").css("color","red")
Ale dowiedziałem się, że jeśli masz wiele spraw do przetestowania, jQuery będzie działać bardzo źle(zwłaszcza na IE6, wiem, jest stary, ale nadal w użyciu). Postanowiłem więc napisać własny filtr atrybutów.
Oto jak go używać: $("li:mcontains('John','Mary')").css("color","red")
Kod
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;
};
3
Author: Kristof,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-14 14:50:07
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-14 14:50:07
It ' s easy:
$("li:contains('John'):contains('Mary')")
1
Author: Slava,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-26 12:33:09
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-26 12:33:09