Home > Article > Web Front-end > Discuss in detail the difference and relationship between id selector and class selector in JQuery
The following editor will bring you a cliché about the difference between jquery id selector and class selector. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
The examples are as follows:
##
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery-2.1.4.js"></script> <script type="text/javascript" src="dams.js"> </script> </head> <body> <p class="box">hello</p> <p class="box">world</p> </body> </html> $(function(){ alert($('.box').size()); //返回2 });
## The #size() method returns the number of DOM objects
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery-2.1.4.js"></script> <script type="text/javascript" src="dams.js"> </script> </head> <body> <p id="box">hello</p> <p id="box">world</p> </body> </html> $(function(){ alert($('#box').size()); //只能获得一个id=box的DOM对象,返回1 });
that is: The id is unique. Even if there are multiple elements with the same id, the jquery selector can only get one of them. So: If you want to set an action on the id in jquery, the id is only allowed to appear once on the page. For CSS style, you can select all DOM objects with id=box in the page:
#box { color: red; };
is very similar, but the function is different: CSS adds a single style after finding the element, while jquery adds an action
behaviorThe above is the detailed content of Discuss in detail the difference and relationship between id selector and class selector in JQuery. For more information, please follow other related articles on the PHP Chinese website!