Difference: 1. The two have different functions. The CSS selector sets the style of the element after finding the element, and the jQuery selector adds behavior after finding the element; 2. The jQuery selector has better cross-browser capabilities. Compatibility; 3. The efficiency of CSS selectors and jQuery selectors is different.
We know that jQuery selectors and CSS selectors are written very similarly, both have the characteristics of implicit iteration, and there is no need to loop through each selection that meets the selector requirements. element, which is relatively convenient to use. Usually, wrapping the css selector with $("") becomes a jQuery selector, such as
CSS Selector | jQuery Selector | |
ID Selector | #myID | $("#myID") |
Class selector | .myClass | $(".myClass") |
Tag selector | p | $("p") |
Level selector | p > strong | $("p>strong") |
jQuery is called a filter selector | p:nth-child(3 )$("p:nth-child(3)") |
<!doctype html> <html> <head> <meta charset="utf-8"> <title>选择器</title> <style type="text/css"> p { font-size: 14px; color:#F00 } p:nth-child(3){color:#690} </style> <script src="jquery/jquery-1.11.3.js"></script> <script> $(document).ready(function() { $("p").css({"color":"#00f","font-size":"16px"}); $("p:nth-child(3)").css({"font-size":"24px"}); }); </script> </head> <body> <p>第一段</p> <p>第二段</p> <p>第三段</p> <p>第四段</p> </body> </html>
So what is the difference between the two?
1. The two have different functions. After the CSS selector finds the element, it sets the style of the element, and the jQuery selector adds the behavior after finding the element. 2. The jQuery selector has better cross-browser compatibility. 3. The efficiency of the selector. Efficiency of CSS selector1. id selector (#myid) 2. Class selector (.myclassname) 3. Tag Selector (p, h1, p) 4, adjacent selector (h1 p) 5, sub-selector (ul > li) 6, Descendant selector (li a) 7, wildcard selector (*) 8, attribute selector (a[rel="external"]) 9, Pseudo-class selector (a:hover,li:nth-child)The efficiency of the above nine selectors is ranked from high to low. The efficiency of the ID selector in the base is the highest, while the pseudo-class The efficiency of the selector is the lowest. For a detailed introduction, you can also view Writing efficient CSS selectors (Address: http://csswizardry.com/2011/09/writing-efficient-css-selectors/). Efficiency of jQuery selector1. id selector $('#id') and element label selector $('form')2. Class selection $('.className')3, attribute selector $('[attribute=value]') and pseudo-class selector $(':hidden')More programming For related knowledge, please visit:Programming Course! !
The above is the detailed content of What is the difference between jquery selectors and CSS selectors?. For more information, please follow other related articles on the PHP Chinese website!