Love beautiful code? We do too.
.toArray(): Lấy tất cả các thành phần DOM chứa trong tập jQuery, như là một mảng (array).
Đã được thêm vào từ phiên bản 1.0
.toArray()
$('li').toArray();
Html viết:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('button').click(function(){
var list = $('li').toArray()
for (i=0;i<list.length;i++){
alert(list[i].innerHTML);
}
});
});
</script>
</head>
<body>
<ul>
<li>Html</li>
<li>Css</li>
<li>jQuery</li>
</ul>
<button>Click</button>
</body>
</html>
Hiển thị trình duyệt:
Ta thấy cách sử dụng .toArray() đã lấy các thành phần li với dạng mãng (array).
Hoclaptrinh.vn © 2017
From Coder With
Unpublished comment
Viết câu trả lời