码迷,mamicode.com
首页 > Web开发 > 详细

JQ和JS获取元素

时间:2017-12-28 16:07:20      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:and   ul li   post   pos   元素   ssi   gpo   ext   element   

<ul>
  <li>John</li>
  <li>Karl</li>
  <li>Brandon</li>
</ul>

获取第一个元素:  $("ul li:first-child")

<div id="getfirst"> 

  <ul>
    <li>John</li>
    <li>Karl</li>
    <li>Brandon</li>
  </ul>

</div>

获取第一个元素:  $("#getfirst").find("ul li:first-child")

或$("#getfirst").find("ul li").get(0)

或$("#getfirst").find("ul li").first()

 

 

 

 

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

JQ获取上一个元素:$("#getfirst").prev()

JQ获取下一个元素:$("#getfirst").next()

JS获取上一个元素:document.getElementById("compcode240").previousSibling

JS获取下一个元素:document.getElementById("compcode240").next

JQ获取第一个子元素:$("#getfirst").find("ul li:first-child")

JS获取第一个子元素:document.getElementById("compcode240").firstChild

 

JQ和JS获取元素

标签:and   ul li   post   pos   元素   ssi   gpo   ext   element   

原文地址:https://www.cnblogs.com/daimaxuejia/p/8135801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!