jQuery contents()
jQuery 中的 contents() 方法用於返回所選元素的所有直接子級(包括文字和註釋節點)。
語法
語法如下 −
$(selector).contents()
示例
讓我們看一個實現 jQuery contents() 方法的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").contents().filter("p").wrap("<i/>");
});
});
</script>
</head>
<body>
<h2>Products</h2>
<div><p>90% of the products are sold.</p></div>
<p>10% of the products are still waiting to get dispatched.</p>
<button>Text Nodes</button><br>
</body>
</html>輸出
這將產生以下輸出 −

現在,點選“文字節點”按鈕 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP