JQuery traversal node Next () method

Source: Internet
Author: User
Tags text dom

Next () method

Next ([selector])

This method gets the collection of elements for the last sibling element of each matching element.
You can filter with an optional expression. Only the adjacent sibling elements are matched to, not all of the subsequent sibling elements.

If you want to get a collection of elements from the previous sibling element of each matching element, see this article jquery tutorial the DOM operation-Traversal Node-prev () method

The Nextall () method and the Nextuntil () method are jquery%e6%95%99%e7%a8%8b%e5%9f%ba%e7%a1%80%e7%af%87%e4%b9%8bdom%e6%93%8d%e4%bd%9c-% e9%81%8d%e5%8e%86%e8%8a%82%e7%82%b9-prevall%e6%96%b9%e6%b3%95/"title=" jquery Tutorial Basic Text DOM Operations-traversal Node-prevall () method
"The >prevall () method and the Prevuntil () method are used in much the same way, only the elements are retrieved backwards, the latter is forward, and the following sections are omitted.

Here's a look at the example:

The code is as follows Copy Code

<UL>
<li>li 1</li>
<li Class=item>li 2</li>
<li>li 3</li>
<li>li 4</li>
</UL>
<UL>
<li>li 1</li>
<li Class=item>li 2</li>
<li>li 3</li>
<li>li 4</li>
</UL>

jquery Code:

$ (' Li.item '). Next (). CSS (' background-color ', ' red ');

The result is that the background of the two UL <li>li 3</li> will turn red.

Example 1

The code is as follows Copy Code

<ul>
<li>jquery 1</li>
<li class= "Item" >php 2</li>
<li>css 3</li>
<li>java 4</li>
</ul>

<ul>
<li>jquery 1</li>
<li class= "Item" >php 2</li>
<li>css 3</li>
<li>java 4</li>
</ul>

<input type= "button" id= "Test" value= "get the latter element of Li with class ' item '" >
<script>
$ ("#test"). Click (function () {
$ (' Li.item '). Next (). CSS (' background-color ', ' red ');
})
</script>

Example 2

The code is as follows Copy Code
<style type= "Text/css" >
div {width:40px; height:40px; margin:10px;
Float:left; BORDER:2PX Blue Solid;
padding:2px; }
span {font-size:14px;}
p {clear:left; margin:10px;} </style><span>
<div id= "Start" >1</div>
<div>2</div>
<div>3<span>span</span></div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</span>
<p><button> Click me to get the next div</button></p>
<script>
var $curr = $ ("#start");
$curr. CSS ("Background", "#f99");
$ ("button"). Click (function () {
$curr = $curr. Next ();
if ($curr. length = = 0) {
$curr = $ ("#start");
}
$ ("div"). CSS ("Background", "");
$curr. CSS ("Background", "#f99");
});
</script>


Finally, let's look at a. Next ([selector]) use with parameters:

The code is as follows Copy Code
<div><span> Dream Ploughing </span></div>
<p> Hello </p>
<p class= "selected" >jquery is great! </p>
<script>
$ ("P"). Next (". Selected"). CSS ("Background", "yellow");
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.