如何使用 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>

更新時間: 14-Feb-2020

98 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始吧
廣告