jQuery中index()方法用法執行個體,jqueryindex

來源:互聯網
上載者:User

jQuery中index()方法用法執行個體,jqueryindex

本文執行個體講述了jQuery中index()方法用法。分享給大家供大家參考。具體分析如下:

此方法可以搜尋匹配元素,並返回元素的索引值。
索引值是從0開始的。

文法結構一:

當此方法沒有參數的時候,傳回值是指定元素在其同輩元素集合中的索引位置。
複製代碼 代碼如下:$(selector).index()

執行個體代碼:

執行個體一:

複製代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.bkjia.com/" />
<title>index()函數-幫客之家</title>
<style type="text/css">
span{
  color:red;
}
</style>
<title>幫客之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $("span").text($(".qian").index());
  })
});
</script>
</head>
<body>
<div>
  <ul>
    <li>後台專區</li>
    <li class="qian">前台專區</li>
    <li>資料庫專區</li>
    <li>站長交流</li>
  </ul>
</div>
<div>當前li元素的位置:<span>0</span></div>
<button id="btn">點擊查看結果</button>
</body>
</html>

上面代碼能夠返回類名為qian的li元素在其同輩元素集合中的索引值,看到這裡大家可能有這樣的疑問,所謂同輩元素是否是同類元素,也就是說li元素在li元素集合中的索引值。

執行個體二:

複製代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.bkjia.com/" />
<title>index()函數-幫客之家</title>
<style type="text/css">
span{
  color:red;
}
</style>
<title>幫客之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $(".index").text($("#sou").index());
  })
});
</script>
</head>
<body>
<div>
  <ul>
    <li>後台專區</li>
    <li id="qian">前台專區</li>
    <li>資料庫專區</li>
    <li>站長交流</li>
    <span id="sou">搜尋最佳化</span>
  </ul>
</div>
<div>當前li元素的位置:<span class="index">0</span></div>
<button id="btn">點擊查看結果</button>
</body>
</html>

由以上的代碼可以看出,並非只是同類元素,而是所有的同輩元素。

文法結構二:

當方法的參數為DOM對象或者jQuery對象時,傳回值是此DOM對象或者jQuery對象在指定的元素集合中索引。
如果在指定的元素集合中找不到指定的DOM對象或者jQuery對象,那麼傳回值為-1。
複製代碼 代碼如下:$(selector).index(element)

參數列表:

參數 描述
element 獲得index位置的DOM對象或者jQuery對象。

執行個體代碼:

執行個體一:

複製代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.bkjia.com/" />
<title>index()函數-幫客之家</title>
<style type="text/css">
span{
  color:red;
}
</style>
<title>幫客之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $("span").text($("li").index(document.getElementById("qian")));
  })
})
</script>
</head>
<body>
<div>
  <ul>
    <li>後台專區</li>
    <li id="qian">前台專區</li>
    <li>資料庫專區</li>
    <li>站長交流</li>
  </ul>
</div>
<div>當前li元素的位置:<span>0</span></div>
<button id="btn">點擊查看結果</button>
</body>
</html>

執行個體二:

因為找不到匹配的元素,所以傳回值是-1.

複製代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.bkjia.com/" />
<title>index()函數-幫客之家</title>
<style type="text/css">
span{
  color:red;
}
</style>
<title>幫客之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $(".index").text($("li").index(document.getElementById("sou")));
  })
});
</script>
</head>
<body>
<div>
  <ul>
    <li>後台專區</li>
    <li id="qian">前台專區</li>
    <li>資料庫專區</li>
    <li>站長交流</li>
    <span id="sou">搜尋最佳化</span>
  </ul>
</div>
<div>當前li元素的位置:<span class="index">0</span></div>
<button id="btn">點擊查看執行個體</button>
</body>
</html>

文法結構三:

當方法的參數為選取器時,將會從通過此選取器獲得的對象集合中尋找元素。
複製代碼 代碼如下:$(selector).index(Jqselector)

參數列表:

參數 描述
Jqselector 選取器,將會從通過此選取器獲得的對象中尋找元素。

執行個體代碼:

複製代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.bkjia.com/" />
<title>index()函數-幫客之家</title>
<style type="text/css">
span{
  color:red;
}
</style>
<title>幫客之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $(".index").text($("#qian").index("li"));
  })
});
</script>
</head>
<body>
<div>
  <ul>
    <li>後台專區</li>
    <li id="qian">前台專區</li>
    <li>資料庫專區</li>
    <li>站長交流</li>
    <span id="sou">搜尋最佳化</span>
  </ul>
</div>
<div>當前li元素的位置:<span class="index">0</span></div>
<button id="btn">點擊查看執行個體</button>
</body>
</html>

上述代碼會取得id值為qian的li元素在通過li選取器獲得li對象集合中的索引位置。

希望本文所述對大家的jQuery程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.