当前位置: 首页 > 文档资料 > jQuery 入门教程 >

text()

优质
小牛编辑
121浏览
2023-12-01

描述 (Description)

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

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.text( ) 

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • NA

例子 (Example)

下面的例子会发现第一段中的文本剥离出html,然后设置第二段的html来显示它只是文本。

<html>
   <head>
      <title>The Selecter Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language="javascript">
         $(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>