Home > Common Problem > body text

What is the difference between jquery selectors and CSS selectors?

青灯夜游
Release: 2020-11-13 15:03:49
Original
5808 people have browsed it

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.

What is the difference between jquery selectors and CSS selectors?

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 is called a pseudo-class selectorp:nth-child(3 )$("p:nth-child(3)")
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
See the example below, CSS is set to 14px for each paragraph text , the color is red, the text is set to 16px in jQuery, and the color is blue, because jquery sets the behavior after the DOM is loaded, all paragraphs appear in blue, 16px words


<!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>
Copy after login

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 selector

1. 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 selector

1. 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template