Example of prev () method usage in jQuery, jqueryprev
This example describes the usage of the prev () method in jQuery. Share it with you for your reference. The specific analysis is as follows:
This method matches the previous peer element next to each element in the element set.
The set of peer elements can be filtered by selector.
Syntax structure:
Copy codeThe Code is as follows: $ (selector). prev (expr)
Parameter List:
| Parameters |
Description |
| Expr |
Optional. Expression used to filter the previous peer Element |
Instance code:
Instance 1:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> prev () function-helper's house </title>
<Style type = "text/css">
. Father {
Height: 200px;
Width: 200px;
Border: 1px solid blue;
}
</Style>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ (". Father p" 2.16.prev(2.16.css ("color", "blue ")
})
</Script>
</Head>
<Body>
<Div class = "father">
<P> I am a p element </p>
<Span> I am a span element </span>
<P> I am a p element </p>
<Div> I am a div element </div>
</Div>
</Body>
</Html>
The above Code sets the font color of the first element of each p element in the p element set under the father element as blue.
Example 2:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> prev () function-helper's house </title>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Li"). prev (". js" ).css ("color", "red ");
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> HTML zone </li>
<Li class = "js"> Javascript area </li>
<Li> Div + Css area </li>
<Li> Jquery area </li>
</Ul>
</Div>
</Body>
</Html>
The above Code sets the font color of the element whose class attribute value is js in front of each li element in all li element sets to red.
I hope this article will help you with jQuery programming.