Home Database Mysql Tutorial Several basic jquery selectors

Several basic jquery selectors

Mar 20, 2018 pm 04:59 PM
jquery Base Selector

The basic selector is the most commonly used selector in jQuery and is also the simplest selector. It searches for DOM elements through element id, class and tag name.

id selector
id selector$('#id') matches an element by the given id, returning a single element

1

<p id="test">测试元素</p><script>//选择id为test的元素并设置其字体颜色为红色$('#test').css('color','red');</script>

Copy after login

Corresponds to the getElementById() method of DOM, and jQuery also uses this method internally to handle the acquisition of ID

1

document.getElementById('test').style.color = 'red';

Copy after login

Element selector
Element selector$('element') is based on the given Matches elements with specified element names and returns qualified collection elements

1

2

3

4

<p>1</p>

<p>2</p>

<script>//选择标签名为p的元素并设置其字体颜色为红色$('p').css('color','red');

</script>

Copy after login

corresponds to the getElementsByTagName() method of DOM, and jQuery also uses this method internally to handle the acquisition of element names

1

2

3

Array.prototype.forEach.call(document.getElementsByTagName('p'),function(item,index,arr){

    item.style.color = 'red';

});

Copy after login

Class selector
Class selector$('.class') matches elements according to the given class name and returns qualified collection elements

1

2

3

4

<p class="test">1</p>

<p class="test">2</p>

<script>//选择class为test的元素并设置其字体颜色为红色$('.test').css('color','red');

</script>

Copy after login

corresponds to DOM’s getElementsByClassName() method, and jQuery also uses this method internally to handle the acquisition of class names

1

2

3

Array.prototype.forEach.call(document.getElementsByClassName('test'),function(item,index,arr){

    item.style.color = 'red';

});

Copy after login

Wildcard selector
The wildcard selector $('*') matches all in the document element, and returns the collection element

1

$('*').css('margin','0');

Copy after login

corresponding to the document.all collection of the DOM

1

2

Array.prototype.forEach.call(document.all,function(item,index,arr){

    item.style.margin = 0;});

Copy after login

or the getElementsByTagName() method whose parameter is the wildcard *

1

2

Array.prototype.forEach.call(document.getElementsByTagName('*'),function(item,index,arr){

    item.style.margin = 0;});

Copy after login

group selection
Group selector $('selector1,selector2,...') merges the elements matched by each selector together, and returns the set element

1

2

3

4

<p class="a">1</p>

<span id="b">2</span>

<a href="#">3</a><script>//选择符合条件的元素并设置其字体颜色为红色$('.a,#b,a').css('color','red');

</script>

Copy after login

corresponding to DOM's querySelectorAll( )Selector

1

2

3

Array.prototype.forEach.call(document.querySelectorAll('.a,#b,a'),function(item,index,arr){

    item.style.color = 'red';

});

Copy after login

The above is the detailed content of Several basic jquery selectors. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Use jQuery to modify the text content of all a tags

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

Understand the role and application scenarios of eq in jQuery

Summary of commonly used file operation functions in PHP Summary of commonly used file operation functions in PHP Apr 03, 2024 pm 02:52 PM

Summary of commonly used file operation functions in PHP

Master jQuery common event binding techniques Master jQuery common event binding techniques Feb 28, 2024 am 08:15 AM

Master jQuery common event binding techniques

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute?

See all articles