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

text()方法获取所有匹配元素的组合文本内容。此方法适用于XML和XHTML文档。

示例

您可以尝试运行以下代码以了解如何在jQuery中使用jQuery.text()方法-

<html>

   <head>
      <title>jQuery text() method</title>
      <script src = "https://cdn.staticfile.org/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>