eq(index) trong jQuery
Miêu tả
Phương thức eq(index) trong jQuery rút gọn tập hợp phần tử được so khớp thành một phần tử đơn.
Cú pháp
Sau đây là cú pháp để sử dụng eq(index) trong jQuery:
selector.eq( index )
Tham số
Chi tiết tham số được sử dụng trong phương thức eq(index) trong jQuery:
index − Đây là vị trí của phần tử trong tập hợp các phần tử được so khớp, bắt đầu tại 0 và tiếp đến bằng độ dài - 1.
Ví dụ
Ví dụ đơn giản sau minh họa cách sử dụng của eq(index) trong jQuery. Nó sẽ thêm màu tới list item thứ hai.
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("li").eq(2).addClass("selected");
});
</script>
<style>
.selected { color:red; }
</style>
</head>
<body>
<div>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
</div>
</body>
</html>
Nó sẽ cho kết quả:
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("li").eq(2).addClass("selected");
});
</script>
<style>
.selected { color:red; }
</style>
</head>
<body>
<div>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li class="selected">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
</ul>
</div>
</body>
</html>
Nó sẽ cho kết quả:
Công cụ giáo viên (2048.VN)
Tạo- trộn đề thi miễn phí từ file PDF, Word, Giao bài nhanh chóng, Hoàn toàn Free
Các bài học jQuery khác tại VietJack:
Dành cho bạn
Công cụ giáo viên
- Tạo đề online từ PDF / Word
- Trộn nhiều mã đề, in Offline
- Giao bài online, Quản lí lớp học

