這種改變原先結果集的方法被稱作destructive jQuery method
jQuery cookbook有如下定義:
A destructive operation is any operation that changes the set of matched jQuery elements, which means any traversing or manipulation method that returns a jQuery object, includingadd(), andSelf(), children(), closes(), filter(), find(), map(),
next(), nextAll(), not(), parent(), parents(), prev(), prevAll(), siblings(), slice(), clone(), appendTo(), prependTo(), insertBefore(), insertAfter(), replaceAll()
。
具體用法如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <p>Text</p> <p class="middle">Middle <span>Text</span></p> <p>Text</p> <script type="text/javascript" src="jquery-1.11.1.js"></script> <script type="text/javascript"> alert($('p').filter('.middle').length); // alerts 1 alert($('p').filter('.middle').end().length); // alerts 3 alert($('p').filter('.middle').find('span').end().end().length); // alerts 3 </script> </body> </html>
當對$('p')
的結果執行filter('.middle')
時,只有<p class="middle">Middle <span>Text</span></p>
符合結果。
繼續對上述操作執行end()
,則filter()
產生的作用被撤銷,結果集中包含三個<p>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com