What is the difference direct descendent (>) vs. d

2019-03-12 15:49发布

What's the difference between these two jQuery statements? They seem to do the same thing by getting all the children div tags.

$("#mainblock div")
$("#mainblock > div")

7条回答
贪生不怕死
2楼-- · 2019-03-12 16:24

$("#mainblock div") find all the divs under #mainblock

$("#mainblock > div") only found its child

suppose you have below HTML structure:

    <div id="mainblock"> 
      <div>
        <div></div> 
        <div></div>
      </div>
     <div></div>
     <div></div>
   </div>

Then

$("#mainblock div").length = 5
$("#mainblock > div").length = 3
查看更多
登录 后发表回答