jQuery: next() method


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

.next ([selector])

Dùng để lấy phần tử nằm ngay sau phần tử hiện thời.

Ví dụ 1: Tìm đến phần tử nằm ngay sau các thẻ <button disabled="disabled"> và đặt vào nội dung "this button is disabled".

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>next demo</title>
<style>
span {
color: blue;
font-weight: bold;
}

button {
width: 100px;
}

</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div><button disabled="disabled">First</button> - <span></span></div>
<div><button>Second</button> - <span></span></div>
<div><button disabled="disabled">Third</button> - <span></span></div>
<script>
$( "button[disabled]" ).next().text( "this button is disabled" );

</script>
</body>
</html>

Ví dụ 2: Tìm đến thẻ nằm ngay sau thẻ <p> có class là "selected" và đặt nền cho thẻ này là màu "yellow".

 <!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>next demo</title>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

</head>

<body>

<p>Hello</p>

<p class="selected">Hello Again</p>

<div><span>And Again</span></div>

<script>

$( "p" ).next( ".selected" ).css( "background", "yellow" );

</script>

</body>

</html> 

» Tiếp: not() method
« Trước: jQuery không thực hiện được sau khi nội dung được load thông qua AJAX
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!