html代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27<!doctype>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>遍历</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="content">
<ul>
<li>一</li>
<li>二</li>
<li>三</li>
</ul>
<ul>
<li>四</li>
<li>五</li>
<li>六</li>
</ul>
</div>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>js
依此点击li输出 0 1 2 3 4 5
1
2
3
4
5
6
7
8$(function(){
// 依次点击li 输出 0 1 2 3 4 5
$('li').each(function(index){
$(this).click(function(){
console.log(index)
})
})
})依次点击li输出 0 1 2 0 1 2
1
2
3
4
5
6
7
8$(function(){
// 一次点击li输出 0 1 2 0 1 2
$('li').each(function(){
$(this).click(function(){
console.log($(this).index());
})
})
})
原文作者:30no2
原文链接:http://mr-9.github.io/2018/01/18/详解jq的each()遍历/
发表日期:January 18th 2018, 12:00:00 am
更新日期:October 10th 2019, 4:48:19 pm
版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可
-
Next Postphp学习导向
-
Previous Posthtml5用户禁止缩放