English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The most basic concept of jQuery is "select some elements and process them".
jQuery selectors allow you to "find" (or select) HTML elements on a webpage.
jQuery supports mostCSS3Selector, as well as some non-standard selectors.
All selectors in jQuery start with a dollar sign and parentheses: $().
jQuery element selectors can be used to select elements based on element names.
You can select all<p>elements as shown below:
$("p")
The following jQuery code will select and highlight all<p>Element:
$("document").ready(function() { $("button").click(function() { $("p").css("background-color", "yellow"); }); });Test and see‹/›
jQuery #idSelector uses the HTML tag'sid attributeto find a specific element.
The ID of an element should be unique in the page, so ID selectors are used to select a unique element.
To find elements with a specific ID, write a hash (#) character followed by the HTML element's ID:
$("#demo")
The following jQuery code will select and highlight the element with id="demo" when the user clicks the button:
$("document").ready(function() { $("button").click(function() { $("#demo").css("background-color", "yellow"); }); });Test and see‹/›
jQuery .classSelector finds elements with a specific class name.
To find elements with a specific class, write a period (.) character followed by the class name:
$(".demo")
The following jQuery code will select and highlight all elements with class="demo" when the user clicks the button:
$("document").ready(function() { $("button").click(function() { $(".demo").css("background-color", "yellow"); }); });Test and see‹/›
You can select multiple selectors as needed. Just separate the selectors with commas.
Multiple selector selects the combination results of all specified selectors:
$("selector"1, "selector"2, "selectorN")
The following jQuery code will select each<h2>,<div>and<span>Element:
$("document").ready(function() { $("h2, "div", "span").css("background-color", "lightgreen"); });Test and see‹/›
This is very useful when you want to perform the same operation on different selectors.
jQuery provides various methods to select specific HTML elements.
Syntax | Description | Example |
---|---|---|
$"*") | Select all elements | demonstration |
$this | Select the current element | demonstration |
$("p.demo") | Select all elements with class="demo"<p>elements | demonstration |
$("p:first") | Select the first<p>elements | demonstration |
$("div p:first-child") | each<div>the first element of<p>elements | demonstration |
$("[target]") | Select elements withtargeteach element with the attribute | demonstration |
$("a[href='/CSS3/']) | each<a>of the elementhrefattribute value equal to " /CSS3/" | demonstration |
$("a[href!='/CSS3/']) | each<a>of the elementhrefattribute value not equal to " /CSS3/" | demonstration |
$":text") | Select all elements of type "text"<input>elements | demonstration |
$("tr:even") | Select all even elements<tr>elements | demonstration |
$("tr:odd") | Select all odd elements<tr>elements | demonstration |
For a complete selector reference, please visit ourjQuery Selectors Reference.