Miałem nadzieję, że $('#childDiv2 .txtClass')
lub $('#childDiv2 input.txtClass')
będzie działać lepiej po wybraniu elementu <input type="text" id="txtID" class="txtClass"/>
. Ale według tego performance analysis$('.txtClass');
jest najlepszym selektorem spośród tego. Używam JQuery 1.7.2 Czy ktoś ma wyjaśnienie tego?Element jquery + wydajność selektora klasy
HTML
<div class="childDiv2">
<input type="text" id="txtID" class="txtClass"/>
<p class="child">Blah Blah Blah</p>
</div>
JS
$('.txtClass');
$('#childDiv2 .txtClass')
$('#childDiv2 > .txtClass')
$('input.txtClass')
$('#childDiv2 input.txtClass')
Ponieważ id musi być unikalny, najszybszym sposobem będzie '$ (" # txtID ")' – Andreas
Moją obawą jest wybór według scenariuszy zajęć? – Lanka
Świetne pytanie. Nie mam odpowiedzi, ale to ciekawe, że nawet podanie kontekstu $ (".txtClass", "# childDiv2") jest wciąż wolniejsze niż selektor klasy. –