jQuery 中的 text( )方法函数

首页 / jQuery入门教程 / jQuery 中的 text( )方法函数

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

text( ) - 语法

selector.text( ) 

text( ) - 示例

下面的示例将在第一段中找到文本,以删除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>

这将产生以下输出-

无涯教程网

This is first paragraph.

This is first paragraph.

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品实战 -〔邱岳〕

数据结构与算法之美 -〔王争〕

性能测试实战30讲 -〔高楼〕

Java业务开发常见错误100例 -〔朱晔〕

SRE实战手册 -〔赵成〕

打造爆款短视频 -〔周维〕

深入剖析Java新特性 -〔范学雷〕

自动化测试高手课 -〔柳胜〕

零基础GPT应用入门课 -〔林健(键盘)〕

好记忆不如烂笔头。留下您的足迹吧 :)