在jQuery中選擇最深的孩子是否有一種廉價的方法來選擇元素中最深的子元素?例:<div id="SearchHere">
<div>
<div>
<div></div>
</div>
</div>
<div></div>
<div>
<div>
<div>
<div id="selectThis"></div>
</div>
</div>
</div>
<div>
<div></div>
</div></div>
3 回答

天涯盡頭無女友
TA貢獻1831條經(jīng)驗 獲得超9個贊
此版本處理沒有子節(jié)點并返回目標本身的情況。
(function($) { $.fn.deepestChild = function() { if ($(this).children().length==0) return $(this); var $target = $(this).children(), $next = $target; while( $next.length ) { $target = $next; $next = $next.children(); } return $target; };}(jQuery));
添加回答
舉報
0/150
提交
取消