Định nghĩa và sử dụng .context trong jQuery

.context: Một DOM node context được thông qua jQuery();, nếu không được thông qua thì khi đó context sẽ có thể là môt document. DOM node có thể hiểu như sau: Theo DOM, tất cả mọi thứ trong tài liệu XML là một node.

 * Toàn bộ văn bản (document) là một document node
 * Mỗi thành phần XML (XML element) là một element node
 * Text trong thành phần XML là text node
 * Mỗi thuộc tính (attribute) là một attribute node
 * Comment là commnet node

Cấu trúc .context trong jQuery

Đã được thêm vào từ phiên bản 1.3

.context
-----------------------------------------------------------------------------------------
'<li>' + $('ul').context + '</li>'

.after(bộ chọn)

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(){
    $('ul').append('<li>' + $('ul').context + '</li>');
    $('ul').append('<li>' + $('ul', document.head).context.nodeName + '</li>');
});
</script>
</head>

<body>
<p>Context:</p>
<ul></ul>
</body>
</html>

Hiển thị trình duyệt:

$('ul').context: cho biết ul là một objectHTMLDocument $('ul', document.head).context: cho biết document.head có tên node là HEAD

Viết câu trả lời

Drop Images

0 Bình luận