Jquery 中 迴圈遍歷 選擇器 each()
阿新 • • 發佈:2019-02-04
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("li").each(function ( index,element){
alert(index);
alert(element);
alert($(this).text())
});
});
});
</script>
</head>
<body>
<button>輸出每個列表項的值</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("li").each(function ( index,element){
alert(index);
alert(element);
alert($(this).text())
});
});
});
</script>
</head>
<body>
<button>輸出每個列表項的值</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
</html>
彈出結果 :
0
[object HTMLLIElement]
Coffee
1
[object HTMLLIElement]
Milk
2
[object HTMLLIElement]
Soda