$(「#foo .bar」)和$(「#foo」)。find(「.bar」)有什麼區別?不同類型的jQuery選擇器
$('#foo').on('click', function(){
$(this).find('.bar').css('background-color', 'yellow');
})
$('#foo_two .bar_two').on('click', function(){
$(this).css('background-color', 'red');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "foo">
<p class= 'bar' style='background: green'> Hello there</p>
</div>
<div id = 'foo_two'>
<p class = 'bar_two' style='background: orange'> Hello there</p>
</div>
在片段中,我試圖勾勒出了我認爲的不同,但似乎到現在還不知道是怎麼回事......
到底綠色標籤的變化?沒有。都使用類欄搜索具有foo ID的元素的後代。 – j08691 2014-10-16 19:45:26
好的,謝謝。所以性能/瀏覽器版本不會受到任何用途的影響? – user3007294 2014-10-16 19:46:47
沒有性能差異,真的。您還應該考慮閱讀[事件授權](http://learn.jquery.com/events/event-delegation/)。 – taveras 2014-10-16 19:47:35