Home  >  Article  >  Web Front-end  >  How to write jquery selector

How to write jquery selector

(*-*)浩
(*-*)浩Original
2019-05-22 17:59:432315browse

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.

How to write jquery selector

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(){
    $(&quot;p&quot;).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(){
    $(&quot;#test&quot;).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(){
    $(&quot;.test&quot;).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!

Statement:
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