jQuery: zaznacza wszystkie elementy danej klasy, z wyjątkiem określonego Id
To chyba całkiem proste.
Chcę wybrać wszystkie elementy danej klasy thisClass
, z wyjątkiem tego, gdzie id to thisId
.
Tzn. coś równoważnego (gdzie - / minus oznacza usunąć):
$(".thisClass"-"#thisId").doAction();
6 answers
Użyj :nie selektor.
$(".thisclass:not(#thisid)").doAction();
Jeśli masz wiele identyfikatorów lub selektorów, użyj ogranicznika przecinków, dodatkowo:
(".thisclass:not(#thisid,#thatid)").doAction();
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
2016-10-25 17:37:27
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
2015-09-21 10:52:16
$(".thisClass[id!='thisId']").doAction();
Dokumentacja selektorów: http://api.jquery.com/category/selectors/
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
2014-12-01 23:30:43
Dorzucę tylko odpowiedź JS (ES6), na wypadek gdyby ktoś jej szukał:
Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => {
doSomething(el);
}
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
2016-03-21 21:32:05
Możesz użyć .nie działa jak w poniższych przykładach, aby usunąć elementy, które mają dokładny id, id zawierające konkretne słowo, id zaczynające się od słowa, itp... zobacz http://www.w3schools.com/jquery/jquery_ref_selectors.asp {[7] } aby uzyskać więcej informacji na temat selektorów jQuery.
Ignoruj przez Exact ID:
$(".thisClass").not('[id="thisId"]').doAction();
Ignoruj ID, które zawiera słowo " Id "
$(".thisClass").not('[id*="Id"]').doAction();
Ignoruj identyfikator zaczynający się od "my"
$(".thisClass").not('[id^="my"]').doAction();
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
2016-03-24 14:29:09
Użycie metody .not()
z zaznaczeniem całego elementu jest również opcją.
Ten sposób może być przydatny, jeśli chcesz wykonać inną akcję bezpośrednio z tym elementem.
$(".thisClass").not($("#thisId")[0].doAnotherAction()).doAction();
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
2016-03-17 13:12:33