如何使用 jQuery 中的 jQuery.text() 方法?


text() 方法獲取所有匹配元素的組合文字內容。此方法適用於 XML 和 XHTML 文件。

範例

你可以嘗試執行以下程式碼,瞭解如何在 jQuery 中使用 jQuery.text() 方法 −

即時演示

<html>

   <head>
      <title>jQuery text() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   
      <script>
         $(document).ready(function() {
            var content = $("p#pid1").text();
            $("#pid2").html(content);
         });
      </script>
       
      <style>
         .red {
            color:red;
         }
         .green {
            color:green;
         }
      </style>
   </head>
   
   <body>
      <p class = "green" id = "pid1">This is <i>first paragraph</i>.</p>
      <p class = "red" id = "pid2">This is second paragraph.</p>
   </body>
   
</html>

更新於:2020 年 2 月 14 日

98 次瀏覽

開啟你的事業

透過完成課程獲得認證

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