html選取器的一些書寫規範

來源:互聯網
上載者:User

標籤:focus   ext   text   class選取器   media   height   span   關係   idt   

  我們都知道常用的選取器有id選取器、class選取器、以及直接標籤選擇,那如果我們想選擇他們的子標籤,而不使用id和class該怎麼選擇呢,這裡我就給大家講一下平時我們在書寫中容易混淆的地方。

  例如我們在選擇p標籤下的span標籤時,我們一般是通過這種寫法來選擇:

  <style media="screen">
    p span{
      color: red;
    }
  </style>

  <p><span>開心 <span>快樂</span></span></p>

  但這樣寫有個問題,會將p標籤下的所有span標籤都設定為紅色,如果我們想將第二個span標籤設定為綠色的話,這裡就需要使用“>”符號來選擇,他作用是一層一層的選擇,表示的是父與子的關係,這時候我們就該這樣寫:

    p>span>span{
      color: yellow;
    }

  這種寫法就可以很好的選擇到我們想選擇的標籤的顏色,同理“>”表示表示的是父子級關係,那如果是兄弟級的話我們就可以使用“+”這個符號來選擇即可。

  例如:我們我們通過點擊input框來改變span的顏色:

  <style media="screen">
    .inputText{
      position: relative;
      width: 100px;
      height: 20px;
    }
    .inputText>input{
      width: 100px;
      height: 20px;
    }
    .inputText>span{
      position: absolute;
      top: 0;
      right: 0;
      width: 10px;
      height: 20px;
      background-color: red;
    }
    .inputText>input:focus +span{
      background-color: green;
    }
  </style> 

  <div class="inputText">
    <input type="text" name="" value="輸入">
    <span></span>
  </div>

 

html選取器的一些書寫規範

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.