欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

老生常談jquery id選擇器和class選擇器的區(qū)別

 更新時間:2017年02月12日 13:00:34   投稿:jingxian  
下面小編就為大家?guī)硪黄仙U刯query id選擇器和class選擇器的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

實例如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link href="style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
  <script type="text/javascript" src="jquery-2.1.4.js"></script>
  <script type="text/javascript" src="dams.js">
  </script>
</head>
<body>
  <div class="box">hello</div>
  <div class="box">world</div>
</body>
</html>



$(function(){
  alert($('.box').size());  //返回2
});

size() 方法返回DOM對象的個數(shù)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link href="style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
  <script type="text/javascript" src="jquery-2.1.4.js"></script>
  <script type="text/javascript" src="dams.js">
  </script>
</head>
<body>
  <div id="box">hello</div>
  <div id="box">world</div>
</body>
</html>





$(function(){
  alert($('#box').size());  //只能獲得一個id=box的DOM對象,返回1
});

即: id是唯一的,即使有多個id相同的元素,jquery選擇器也只能獲取其中一個 。所以:想在jquery中對id設(shè)置動作, id在頁面中只允許出現(xiàn)一次。

對于CSS樣式來說,可以選取頁面中所有id=box的DOM對象:

#box {

  color: red;


};

jQuery選擇器的寫法和CSS選擇器十分類似,但是功能卻不同:

CSS找到元素后添加的是單一樣式,而jquery添加的是動作行為。

以上這篇老生常談jquery id選擇器和class選擇器的區(qū)別就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論