1. 程式人生 > >Jquery 中 迴圈遍歷 選擇器 each()

Jquery 中 迴圈遍歷 選擇器 each()

<!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>

彈出結果 :

   0

[object HTMLLIElement]

Coffee

1

[object HTMLLIElement]

Milk

2

[object HTMLLIElement]

Soda