jQuery遍历之next()、nextAll()方法使用实例

  jquery遍历:next()和nextAll()方法。实例如下:

  

复制代码 代码如下:

  <html>

  <head>

  <script type="text/javascript" src="jquery-1.8.2.min.js"></script>

  <script type="text/javascript">

  $(document).ready(function(){

  //$("div").click(function(){alert($(this).next().text());});

  //$("div").click(function(){alert($(this).nextAll().text());});

  $("div").click(function(){alert($(this).nextAll("div").text());});

  });

  </script>

  <style type="text/css">

  div{width:300px;height:30px;background:green;margin-top:10px;}

  </style>

  </head>

  <body>

  <div id="uu">您好,<font color="blue">美女</font></div>

  <div>hello,world</div>

  <div>美女,亲一下</div>

  <p>我是p标签</p>

  <div><span>我很帅,有图有真相</span></div>

  <p>我也是p标签</p>

  </body>

  </html>

  说明:

  (1)next()方法:是指获得匹配元素的相邻同同辈元素(即下一个同辈元素),注意,同辈元素并不是标签相同的元素,而是指该元素闭合后的下一个元素,如实例中的”<div>美女,亲一下</div>“,div闭合后的下一个元素为<p>。

  (2)如果next()加上参数,即next("div"),如果下一个相邻元素不是div,则为空,即一定是相邻的。

  (3)nextAll()方法:指获得匹配元素之后所有的同辈元素。它同样可以加上参数,nextAll("p")或nextAll("div")等等。

  (4)有些奇怪的是,如果是这样的代码:

  

复制代码 代码如下:

  $("div").click(function(){alert($(this).nextAll("div").html());});

  它并不能获得所有的html内容,而只是获得下一个同辈元素的html内容。???疑惑