Several selectors and examples of jQuery detailed explanation
The jQuery selector allows you to manipulate groups of HTML elements or individual elements.
jQuery selector
The jQuery selector allows you to manipulate groups of HTML elements or individual elements.
The jQuery selector "finds" (or selects) HTML elements based on their id, class, type, attribute, attribute value, and so on. It is based on existing CSS selectors, in addition to which it has a number of custom selectors.
All selectors in jQuery start with a dollar sign: $().
Element selector
The jQuery element selector selects elements based on their name.
Select all <p> elements in the page:
$("p")
Living example
After the user clicks the button, all <p> elements are hidden:
Living example
$(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); });
#id selector
The jQuery #id selector selects the specified element through the id attribute of the HTML element.
The id of the element on the page should be unique, so you need to go through the #id selector to select the unique element on the page.
The syntax for selecting elements by id is as follows:
$("#test")
Living example
When the user clicks the button, the element with the id="test" attribute is hidden:
Living example
$(document).ready(function(){ $("button").click(function(){ $("#test").hide(); }); });
.class selector
The jQuery class selector can look for elements by the specified class.
The syntax is as follows:
$(".test")
Living example
All elements with the class="test" attribute are hidden after the user clicks the button:
Living example
$(document).ready(function(){ $("button").click(function(){ $(".test").hide(); }); });
More examples
grammar | Description | Living example |
$(" * ") | Select all elements | Online instance |
$(this) | Selects the current HTML element | Online instance |
$("p.intro") | Select the <p> element whose class is intro | Online instance |
$("p:first") | Select the first <p> element | Online instance |
$("ul li:first") | Select the first <li> element of the first <ul> element | Online instance |
$("ul li:first-child") | Selects the first <li> element of each <ul> element | Online instance |
$("[href]") | Select the element with the href attribute | Online instance |
$("a[target='_blank']") | Selects all <a> elements whose target attribute value is equal to "_blank" | Online instance |
$("a[target!='_blank']") | Selects all <a> elements whose target attribute value is not equal to "_blank" | Online instance |
$(":button") | Select all <input> elements and <button> elements whose type="button" | Online instance |
$("tr:even") | Select the <tr> element in an even position | Online instance |
$("tr:odd") | Select the <tr> element in an odd position | Online instance |
Use jQuery functions in separate files
If your website contains many pages and you want your jQuery functions to be easy to maintain, put your jQuery functions in a separate.js file.
When we demonstrate jQuery in this tutorial, we add functions directly to the <head> section. However, it would be better to put them in a separate file, like this (using the src attribute to reference the file) :
Living example
The < head > < script SRC = "/ / cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js" > < / script > < script src="my_jquery_functions.js"></script> </head>
To this article about jQuery several selectors detailed explanation of the article is introduced to this, more related jQuery selector content please search script House previous articles or continue to browse the following related articles hope that you will support script House in the future!
Related article
-
A very simple jquery+xml+ajax no refresh tree structure (no css, background is c#)
A very simple jquery+xml+ajax no refresh tree structure (no css, background is c#)2010-06-06 -
jquery implements parity rows to assign different css values
In order to make the page more beautiful, we often like to add different colors to the parity lines2012-02-02 -
jQuery snow animation effect example [with source code download]
This article mainly introduces the snow animation effect achieved by jQuery, involving jQuery plug-in combined with setInterval, animate animation operation related skills, and attached source code for readers to download reference, need friends can refer to2018-02-02 -
jquery implements asynchronous loading images (lazy loading images a way)
This article mainly introduces the jquery implementation of asynchronous loading (lazy loading image a way), has a certain reference value, interested partners can refer to it.2017-04-04 -
Jsonarray.js method to get the map-based JSONArray returned via ajax
The following is js to obtain the detailed code of the map type JSONArray returned by ajax, and the friends who need it can refer to it2014-01-01 -
jQuery toggles all checkbox selection methods
This article mainly introduces the jQuery method to switch all check box selected state, can achieve batch switch check box selected state function, need friends can refer to the next2015-07-07 -
jQuery simplePage+AJAX plus page plug-in Usage example
This article mainly introduces the usage of jQuery simplePage+AJAX plus paging plug-in, combined with examples to analyze the specific skills of jQuery based on extend extension to achieve paging plug-in, need friends can refer to2016-02-02 -
jQuery implements the floating layer plug-in that can be closed by clicking the button
This article mainly introduces jQuery to realize the floating layer plug-in that can be closed by clicking the button, which has the floating layer effect of clicking and popping up with the mask layer, and the floating layer can be dragged and closed, and the friends who need it can refer to it2015-09-09 -
Basic knowledge of jquery first lesson understanding jquery
jquery basic knowledge first talk first meet, have a preliminary understanding of jquery, lay a foundation for later learning, interested partners can refer to it2016-03-03
Latest comments