The difference and connection between CSS selector and jquery selector

Source: Internet
Author: User

We know that the jquery selector and CSS selectors are very similar in style, with implicit iterative features that do not need to iterate through each element that meets the selector requirements, and are relatively convenient to use, typically wrapping a CSS selector with $ ("") as a jquery selector, such as

CSS Selector jquery Selector
ID Selector #myID $ ("MyID")
Class Selector . myClass $ (". MyClass")
Tag Selector P $ ("P")
Hierarchy Selector div > Strong $ ("Div>strong")
CSS is called pseudo class selector
jquery is called a filter selector.
P:nth-child (3) $ ("P:nth-child (3)")

See the following example, CSS for each paragraph of the text set to 14PX, the color is red, jquery set the text to 16PX, the color is blue, because jquery is set to the DOM loading behavior, all the paragraphs appear in blue, 16px words

<!doctype html>
<meta charset= "Utf-8" >
<title> Selectors </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>
<body>
<p> First Segment </p>
<p> Second Segment </p>
<p> Third Segment </p>
<p> Fourth Segment </p>
</body>

So what's the difference between the two?

1, but the role of the two is different, the CSS selector after finding the element to set the style of the element, the jquery selector to find the element after adding behavior.

2. The jquery selector has better cross-browser compatibility.

3, the efficiency of the selector.

The efficiency of CSS selectors

    1. ID selector (#myid)
    2. Class selector (. myclassname)
    3. Tag Selector (div,h1,p)
    4. Adjacent selector (h1+p)
    5. Sub-selector (UL < LI)
    6. Descendant selector (Li a)
    7. Wildcard selector (*)
    8. Property selector (a[rel= "external"])
    9. Pseudo-Class selector (A:hover,li:nth-child)

The efficiency of the above nine selectors is high to low, and the efficiency of the ID selector is the highest in the base, while the efficiency of the pseudo-class selector is the bottom line. Detailed introduction You can also click Writing efficient CSS selectors.

The efficiency of the jquery selector

    1. ID selector $ (' #id ') and element tag Selector $ (' form ')
    2. Class Selector $ ('. ClassName ')
    3. Property selector $ (' [Attribute=value] ') and pseudo class selector $ (': Hidden ')

The difference and connection between CSS selector and jquery selector

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.