jquery选择器空格与大于号、加号与波浪号的区别(转)

空格:$('parent childchild')表示获取parent下的所有的childchild节点,所有的子孙。

大于号:$('parent child')表示获取parent下的所有child的儿子,第一代。

加号:$('pre + nextbrother')表示获得pre节点的下一个兄弟节点,相当于next()方法

波浪号:$('pre ~ brother')表示获取pre节点的后面的所有兄弟节点,相当于nextAll()方法。

下面是一个小例子,看看他们的区别吧~~~~

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
   <script src='f:/study/jquery.js'></script>
      <script>
      $(function(){
       
      $("#b").append($("#a span").clone());
   $("#c").append($("#span12 + span").clone());
   $("#d").append($("#span12 ~ span").clone());
   $("#e").append($("#a > span").clone());
   
   })
   
   </script>
 </head>

 <body>
  <div id='a'>
       <span>span1</span>
    <div>
      <span>span1.1</span>
   <span id='span12'>span1.2</span>
   <span>span1.3</span>

    <span>span1.4</span>
     <span>span1.5</span>
      <span>span1.6</span>
    </div>
    <span>span2</span>
    <div>
      <span>span2.1</span>
   <span>span2.2</span>
   <span>span2.3</span>
    <span>span2.4</span>
     <span>span2.5</span>
      <span>span2.6</span>
    </div>
    <span>span3</span>
    <span>span4</span>
  
  
  </div>


 <hr>
  <div id='b'></div>
      空格:
  <hr>
  <div id='c'></div>
+:
  <hr>
  <div id='d'></div>
~:
  <hr>
  <div id='e'>
  >
  </div>
 </body>
</html>

相关推荐