This example describes the jquery search sibling element approach. Share to everyone for your reference. The specific analysis is as follows:
1. Next () method
Used to search for a single sibling element immediately after each matching element, optionally specifying a selector to filter sibling elements as follows:
Copy Code code as follows:
Next ([selector])
$ ("P"). Next ("P"). CSS ("Color", "#FCF");
2. Nextall () method
Used to search for all sibling elements immediately following each matching element, optionally specifying a selector to filter sibling elements
Copy Code code as follows:
Nextall ([selector])
$ ("P"). Nextall (). CSS ("Color", "blue");
3. Nextutil () method
Used to get the sibling element immediately following each matching element until the element that matches the given selector, but not the element, is formatted as follows:
Copy Code code as follows:
Instance:
Copy Code code as follows:
$ ("#div1"). Nextutil ("div"). CSS ("Border", "1px solid Red");
Set the border of all middle sibling elements with ID div1 to the end of the next DIV element to red
4. Prev () method
Used to search for a single sibling element immediately preceding each matching element, optionally specifying a selector to filter the sibling elements, in the following syntax format:
Copy Code code as follows:
Instance:
Copy Code code as follows:
$ ("#div2"). Prev ("span"). CSS ("Color", "blue");
The font for a SPAN element immediately preceding the DIV2 element becomes blue.
5. Prevall () method
Used to search for all sibling elements before the current element, or you can specify a selector to filter sibling elements. The syntax format is as follows:
Copy Code code as follows:
6. Prevutil () method
Used to search all sibling elements before the current element until the matching element is encountered, the syntax is as follows:
Copy Code code as follows:
Prevutil ([selector])
$ ("#div2"). Prevutil ("input"). CSS ("Color", "red");
7. Siblings () method
Used to search for all sibling elements of each matching element, optionally specifying a selector to filter those sibling elements. The format is as follows:
Copy Code code as follows:
Siblings ([selector])
$ ("div"). Siblings (). CSS ("Color", "red");
Examples are as follows:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script src= "Jquery-1.4.2.js" type= "Text/javascript" ></script>
<title> search for sibling elements of the specified element </title>
<script language= "javascript" type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#td1"). Next (). CSS ("Color", "red"); Set the font color for the next sibling element of the TD element
$ ("#td1"). Nextall (). CSS ("Border", "1px solid Blue");//Set a border on all sibling elements that follow the TD element
$ ("#td2"). Prev (). CSS ("Color", "#99c"); Set the font color for one of the preceding sibling elements of the TD element
$ ("#td2"). Prevall (). CSS ("Background", "#FCF");//Set background to all sibling elements before the TD element
$ ("#td3"). Siblings (). CSS ("Color", "#99F"); Sets the font color for the sibling elements of the specified element in the current realm
})
</script>
<body>
<table width= "637" height= "351" border= "1" >
<tr>
<TD width= "id=" TD1 > Single room <span> (live) </span></td>
<TD width= "112" > Double room <span> (not live) </span></td>
<TD width= "134" > Deluxe Suite <span> (live) </span></td>
<TD width= "117" > Single room <span> (live) </span></td>
<TD width= > Deluxe Suite <span> (not live) </span></td>
</tr>
<tr>
<td> single room <span> (live) </span></td>
<td> Double room <span> (not staying) </span></td>
<TD id= "TD2" > Deluxe suite <span> (not live) </span></td>
<td> single room <span> (not live) </span></td>
<td> Deluxe Suite <span> (not live) </span></td>
</tr>
<tr>
<td> single room <span> (not live) </span></td>
<td> Double room <span> (not staying) </span></td>
<TD id= "TD3" > Deluxe Suite <span> (live) </span></td>
<td> single room <span> (live) </span></td>
<td> Deluxe Suite <span> (not live) </span></td>
</tr>
<tr>
<td> single room <span> (live) </span></td>
<td> Double Room <span> (live) </span></td>
<td> Deluxe Suite <span> (not live) </span></td>
<td> single room <span> (not live) </span></td>
<td> Deluxe Suite <span> (live) </span></td>
</tr>
<tr>
<td> single room <span> (live) </span></td>
<td> Double room <span> (not staying) </span></td>
<td> Deluxe Suite <span> (live) </span></td>
<td> single room <span> (live) </span></td>
<td> Deluxe Suite <span> (not live) </span></td>
</tr>
</table>
</body>
The effect chart is as follows:
I hope this article will help you with your jquery programming.