What is the difference between jquery selectors and CSS selectors?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor