
批改狀態(tài):合格
老師批語:
:first偽類選擇器相當于:eq(0)。它也可以寫為:lt(1)。雖然:first只匹配一個單獨的元素,但是:first-child選擇器可以匹配多個:即為每個父級元素匹配第一個子元素。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>學習筆記-Jquery</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div {
width: 500px;
height: 60px;
border: 1px solid;
border-color: #000000;
margin: 15px auto 0 auto;
text-align: center;
}
.addCss {
border-color: orangered;
background: red;
color: #FFF;
font-size: 30px;
}
p {
width: 500px;
height: 50px;
text-align: center;
line-height: 50px;
margin: 30px auto 0 auto;
border: 1px solid #eee;
background: #0356fa;
cursor: pointer;
color: #FFF;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<p type="button" onclick="clicks()">:first (點我選擇第一個DIV)</p>
</body>
<script type="text/javascript">
function clicks(){
$('div:first').addClass('addCss');
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>學習筆記-Jquery</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div {
width: 500px;
height: 60px;
border: 1px solid;
border-color: #000000;
margin: 15px auto 0 auto;
text-align: center;
}
.addCss {
border-color: orangered;
background: red;
color: #FFF;
font-size: 30px;
line-height: 60px;
}
p {
width: 500px;
height: 50px;
text-align: center;
line-height: 50px;
margin: 30px auto 0 auto;
border: 1px solid #eee;
background: #0356fa;
cursor: pointer;
color: #FFF;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<p type="button" onclick="clicks()">$('div:last')(點我選擇最后一個DIV)</p>
</body>
<script type="text/javascript">
function clicks(){
$('div:last').addClass('addCss');
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>學習筆記-Jquery</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div {
width: 500px;
height: 60px;
border: 1px solid;
border-color: #000000;
margin: 15px auto 0 auto;
text-align: center;
}
.addCss {
border-color: orangered;
background: red;
color: #FFF;
font-size: 30px;
line-height: 60px;
}
p {
width: 500px;
height: 50px;
text-align: center;
line-height: 50px;
margin: 30px auto 0 auto;
border: 1px solid #eee;
background: #0356fa;
cursor: pointer;
color: #FFF;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<p type="button" onclick="clicks()"> $('div:eq(2)')(點我選擇) </p>
</body>
<script type="text/javascript">
function clicks(){
$('div:eq(2)').addClass('addCss');
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>學習筆記-Jquery</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div {
width: 500px;
height: 60px;
border: 1px solid;
border-color: #000000;
margin: 15px auto 0 auto;
text-align: center;
}
.addCss {
border-color: orangered;
background: red;
color: #FFF;
font-size: 30px;
line-height: 60px;
}
p {
width: 500px;
height: 50px;
text-align: center;
line-height: 50px;
margin: 30px auto 0 auto;
border: 1px solid #eee;
background: #0356fa;
cursor: pointer;
color: #FFF;
}
</style>
</head>
<body>
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<p type="button" onclick="clicks()"> $('div:lt(2)')(點我選擇) </p>
</body>
<script type="text/javascript">
function clicks(){
$('div:lt(2)').addClass('addCss');
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>學習筆記-Jquery</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div {
width: 500px;
height: 60px;
border: 1px solid;
border-color: #000000;
margin: 15px auto 0 auto;
text-align: center;
}
.addCss {
border-color: orangered;
background: red;
color: #FFF;
font-size: 30px;
line-height: 60px;
}
p {
width: 500px;
height: 50px;
text-align: center;
line-height: 50px;
margin: 30px auto 0 auto;
border: 1px solid #eee;
background: #0356fa;
cursor: pointer;
color: #FFF;
}
</style>
</head>
<body>
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<p type="button" onclick="clicks()"> $('div:gt(0)')(點我選擇) </p>
</body>
<script type="text/javascript">
function clicks(){
$('div:gt(0)').addClass('addCss');
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>學習筆記-Jquery</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div {
width: 500px;
height: 60px;
border: 1px solid;
border-color: #000000;
margin: 15px auto 0 auto;
text-align: center;
}
.addCss {
border-color: orangered;
background: red;
color: #FFF;
font-size: 30px;
line-height: 60px;
}
p {
width: 500px;
height: 50px;
text-align: center;
line-height: 50px;
margin: 30px auto 0 auto;
border: 1px solid #eee;
background: #0356fa;
cursor: pointer;
color: #FFF;
}
</style>
</head>
<body>
<div id="a">0</div>
<div id="b">1</div>
<div id="c">2</div>
<div class="d">3</div>
<div id="e">4</div>
<div class="f">5</div>
<p type="button" onclick="clicks()"> $('div[id]')(點我選擇有ID屬性的DIV) </p>
</body>
<script type="text/javascript">
function clicks(){
$('div[id]').addClass('addCss');
}
</script>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號