首页 > 代码库 > jquery closest方法

jquery closest方法

.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>