您的位置:首页 > Web前端 > JQuery

jquery closest方法

2014-05-07 11:05 288 查看


.closest( selector )

Description: For
each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

使用说明:

对于集合(set)中的每一个元素, 通过在文档树中向上遍历这个元素本身和它的祖先元素,来取得第一个与选择符(selector)匹配的那个元素.

举例:

<ul  style="float:left">
<li >
<table>
<tr>
<td>
this is <span>[Span]</span> party.
<div>1
<div>2
<article>
<span id="third">3
<div>4
<div>5</div>
</div>
</span>
</article>
</div>
</div>
</td>
</tr>
</table>
</li>
</ul>


$("#third").closest("div")

结果为:

<div>2
<article>
<span id="third">3
<div>4
<div>5</div>
</div>
</span>
</article>
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: