亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

class問題

<!DOCTYPE html>

<html>


<head>

? ? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

? ? <title></title>

? ? <link rel="stylesheet" href="imooc.css" type="text/css">

? ? <script src="https://libs.baidu.com/jquery/1.9.1/jquery.js"></script>

</head>


<body>

? ? <h2>子元素篩選選擇器</h2>

? ? <h3>:first-child、:last-child、:only-child</h3>

? ? <div class="left first-div">

? ? ? ? <div class="div">

? ? ? ? ? ? <a>:first-child</a>

? ? ? ? ? ? <a>第二個元素</a>

? ? ? ? ? ? <a>:last-child</a>

? ? ? ? </div>

? ? ? ? <div class="div">

? ? ? ? ? ? <a>:first-child</a>

? ? ? ? </div>

? ? ? ? <div class="div">

? ? ? ? ? ? <a>:first-child</a>

? ? ? ? ? ? <a>第二個元素</a>

? ? ? ? ? ? <a>:last-child</a>

? ? ? ? </div>

? ? </div>


? ? <script type="text/javascript">

? ? ? ? //查找class="first-div"下的第一個a元素

? ? ? ? //針對所有父級下的第一個

? ? ? ? $('.first-div a:first-child').css("color", "#CD00CD");

? ? </script>


? ? <script type="text/javascript">

? ? ? ? //查找class="first-div"下的最后一個a元素

? ? ? ? //針對所有父級下的最后一個

? ? ? ? //如果只有一個元素的話,last也是第一個元素

? ? ? ? $(".first-div a:last-child").css("color", "red");

? ? </script>


? ? <script type="text/javascript">

? ? ? ? //查找class="first-div"下的只有一個子元素的a元素

? ? ? ? $(".first-div a:only-child").css("color", "blue");

? ? </script>



? ? <h3>:nth-child、:nth-last-child</h3>

? ? <div class="left last-div">

? ? ? ? <div class="div">

? ? ? ? ? ? <a>:first-child</a>

? ? ? ? ? ? <a>第二個元素</a>

? ? ? ? ? ? <a>第三個元素</a>

? ? ? ? ? ? <a>:last-child</a>

? ? ? ? </div>

? ? ? ? <div class="div">

? ? ? ? ? ? <a>:first-child</a>

? ? ? ? ? ? <a>第二個元素</a>

? ? ? ? </div>

? ? ? ? <div class="div">

? ? ? ? ? ? <a>:first-child</a>

? ? ? ? ? ? <a>第二個元素</a>

? ? ? ? ? ? <a>第三個元素</a>

? ? ? ? ? ? <a>:last-child</a>

? ? ? ? </div>

? ? </div>


? ? <script type="text/javascript">

? ? ? ? //查找class="last-div"下的第二個a元素

? ? ? ? $(".last-div a:nth-child(2)").css("color", "#CD00CD");

? ? </script>


? ? <script type="text/javascript">

? ? ? ? //查找class="last-div"下的倒數第二個a元素

? ? ? ? $(".last-div a:nth-last-child(2)").css("color", "red");

? ? </script>


</body>


</html>




上面代碼中加粗的? ?class不是等于"left first-div"嘛? ?和下面加粗的代碼不應該寫成$(".left first-div ....")嗎?

正在回答

1 回答

left和first-div 是兩個樣式? ?他后面的只是要查找class="first-div"下的第一個a元素

1 回復 有任何疑惑可以回復我~
#1

Flay_Away 提問者

謝謝你,我知道了!不管是class或者是id它們等于后面加空格的話,都相當于同一個元素給了兩種樣式,是吧。
2018-09-10 回復 有任何疑惑可以回復我~
#2

Flay_Away 提問者

看到后面有講的,哈哈。
2018-09-10 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號