Home > Web Front-end > JS Tutorial > body text

The difference between spaces and greater than sign >, plus sign + and tilde ~ in jquery selector

黄舟
Release: 2017-03-01 14:47:32
Original
1355 people have browsed it

Concept

##Space: $('parent childchild') means to get all childchild nodes under parent

##Greater than sign: $('parent >

childchild

') means to get all the lower levels under parent childchildPlus sign: $('pre + nextbrother') means to get the next sibling node of the pre node, which is equivalent to the next() method

Tilde sign: $('pre ~ brother') means to get All sibling nodes behind the pre node are equivalent to the nextAll() method

Detailed description

The existing code is as follows

<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<p id="imgs_box">
	<ul class="play_imgs_width  imgs_source">
		<li><a href="javascript:;"><img src="./images/banner1.jpg" idth="610" height="390"/></a></li>
		<li><a href="javascript:;"><img src="./images/banner1.jpg" idth="610" height="390"/></a></li>
		<li><a href="javascript:;"><img src="./images/banner1.jpg" idth="610" height="390"/></a></li>
	</ul>
	<ul class="imgs_buttons play_imgs_width">
		<li><a href="" class="buttons_ahover">1</a></li>
		<li><a href="" class="buttons_default">2</a></li>
		<li><a href="" class="buttons_default">3</a></li>
	</ul>	
	<ul class="test">
		<li>
			<ul class="test_first_child">
				<li></li>
				<li></li>
				<li></li>
				<li></li>
			</ul>
		</li>
	</ul> 
</p>
Copy after login

Use of spaces

If you want to get all a tags in imgs_box, you can use spaces, the code is as follows

//获取imgs_box下的所有元素
$(function(){
	$(&#39;#imgs_box a&#39;).each(function(){
		console.log(this);
	});
});
Copy after login

The effect is as shown below, As you can see, all elements were obtained

##Use of greater than sign


If you want all ul elements at the next level in imgs_box, excluding elements with class test_first_child, you can use the following code

$(function(){
	$(&#39;#imgs_box > ul&#39;).each(function(){
		console.log(this);
	});
});
Copy after login


##Use of plus sign

#If you want to get the next adjacent element of the imgs_source element, you can use the plus sign. The code is as follows

$(function(){
	$(&#39;.imgs_source + ul&#39;).each(function(){
		console.log(this);
	});
});
Copy after login


Use of tilde

If you want to get all the sibling elements of the class

imgs_source element, you can use the tilde~. The code is as follows


##

$(function(){
	$(&#39;.imgs_source ~ ul&#39;).each(function(){
		console.log(this);
	});
});
Copy after login


The above is the difference between the space and the greater than sign >, the plus sign + and the tilde ~ in the jquery selector. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!