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>

輸出

這將產生以下輸出 −

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

更新日期:2019-11-11

124 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.