jQuery basic selector (2)

Basic Selector

2.png

Group Selector

Please see the code below:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>选择器</title>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("div,p,span").css("color","red");
		})
	</script>
</head>
<body>
	<div>php 中文网</div>
	<div>php 中文网</div>
	<div>php 中文网</div>

	<br><br>

	<p>php.cn</p>
	<p>php.cn</p>
	<p>php.cn</p>


	<br><br>
	<span>php 中文网</span>
	<span>php 中文网</span>
	<span>php 中文网</span>
</body>
</html>

Please look at the following example

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>选择器</title>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			
		})
	</script>
</head>
<body>
	<div id="dv">php 中文网</div>
	<div>php 中文网</div>
	<div>php 中文网</div>

	<br><br>

	<p>php.cn</p>
	<p class="p1">php.cn</p>
	<p class="p1">php.cn</p>


	<br><br>
	<span>php 中文网</span>
	<span>php 中文网</span>
	<span>php 中文网</span>
</body>
</html>

Use the group method to make the id div, the class is p1 and the span tag turns red

The code is as follows:

$("#dv,.p1,span").css("color","red");

Descendant selector

Let’s write an example below:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>选择器</title>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			
		})
	</script>
</head>
<body>
	<ul>
		<li><a href="#">php 中文网</a></li>
		<li><a href="#">php 中文网</a></li>
	</ul>

	<a href="#">php 中文网</a>
	<a href="#">php 中文网</a>
</body>
</html>

Please look at the above code, I want the color of the a tag of the li tag to turn red

We can write like this, the code is as follows:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>选择器</title>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("ul li a").css("color","red");
		})
	</script>
</head>
<body>
	<ul>
		<li><a href="#">php 中文网</a></li>
		<li><a href="#">php 中文网</a></li>
	</ul>

	<a href="#">php 中文网</a>
	<a href="#">php 中文网</a>
</body>
</html>

Look at the above code We $("ul li a").css("color","red");

In fact, we can write it as $("ul a").css("color","red");, but for the sake of accuracy, it is better if we write it as the Plenary Session,

For example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>选择器</title>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			
		})
	</script>
</head>
<body>
	<ul>
		<li><a href="#">php 中文网</a></li>
		<li><a href="#">php 中文网</a></li>
		<a href="#">php.cn</a>
	</ul>

	<a href="#">php 中文网</a>
	<a href="#">php 中文网</a>
</body>
</html>

Looking at the above code, if we write $("ul a").css("color","red"); then the a tag under ul will turn red

But what we initially want is for the a tag under the li tag to turn red

So, at this time we need to write the full name $("ul li a").css("color","red ");

Wildcard selector *

Look at the following code:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>选择器</title>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("*").css("color","red");
		})
	</script>
</head>
<body>
	<ul>
		<li><a href="#">php 中文网</a></li>
		<li><a href="#">php 中文网</a></li>
		<a href="#">php.cn</a>
	</ul>

	<a href="#">php 中文网</a>
	<a href="#">php 中文网</a>


	<p> php.cn </p>
	<p> php.cn </p>
	<p> php.cn </p>


	<span>php 中文网</span>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>选择器</title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("div,p,span").css("color","red"); }) </script> </head> <body> <div>php 中文网</div> <div>php 中文网</div> <div>php 中文网</div> <br><br> <p>php.cn</p> <p>php.cn</p> <p>php.cn</p> <br><br> <span>php 中文网</span> <span>php 中文网</span> <span>php 中文网</span> </body> </html>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!