jquery is a Js framework that simplifies many js writing methods, making it simpler and more efficient. So, how to write jquery selectors? Here are several commonly used selectors for your reference.
jQuery Selectors
jQuery selectors allow you to operate on groups of HTML elements or on individual elements.
jQuery selectors "find" (or select) HTML elements based on the element's id, class, type, attributes, attribute values, etc. It is based on existing CSS selectors, in addition to some custom selectors.
All selectors in jQuery begin with a dollar sign: $().
Element Selector
jQuery element selector selects elements based on their name.
Select all
elements in the page:
$("p")
Example
After the user clicks the button, all elements are hidden:
元素选择器 <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); </script>这是一个标题
这是一个段落。
这是另一个段落。
#id selector
jQuery #id selector selects the specified element through the id attribute of the HTML element.
The id of the element in the page should be unique, so if you want to select the only element in the page, you need to use the #id selector.
The syntax for selecting elements by id is as follows:
$("#test")
Example
When the user clicks the button, the element with the id="test" attribute will be hidden:
#id 选择器 <script> $(document).ready(function(){ $("button").click(function(){ $("#test").hide(); }); }); </script>这是一个标题
这是一个段落
这是另外一个段落
.class Selector
jQuery class selector can find elements by specifying class.
The syntax is as follows:
$(".test")
Instance
After the user clicks the button, all elements with the class="test" attribute are hidden:
.class 选择器 <script> $(document).ready(function(){ $("button").click(function(){ $(".test").hide(); }); }); </script>这是一个标题
这是一个段落。
这是另外一个段落。
The above is the detailed content of How to write jquery selector. For more information, please follow other related articles on the PHP Chinese website!