jQuery 中的 html( )方法函数

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

html()方法获取第一个匹配元素的html内容。此属性在XML文档上不可用,但适用于XHTML文档。

html( ) - 语法

selector.html()

html( ) - 示例

以下示例将获取第一段的HTML内容,并将其显示在第二段中。请同时检查 html(val)方法的描述。

<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").html();
            $("#pid2").html( content );
         });
      </script>
		
      <style>
         .red { color:red; }
         .green { color:green; }
      </style>
   </head>
	
   <body>
      <p class="green" id="pid1">This is first paragraph.</p>
      <p class="red" id="pid2">This is second paragraph.</p>
   </body>
</html>

这将产生以下输出-

This is first paragraph.

This is first paragraph.

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

技术教程推荐

快速上手Kotlin开发 -〔张涛〕

黄勇的OKR实战笔记 -〔黄勇〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

如何看懂一幅画 -〔罗桂霞〕

Flink核心技术与实战 -〔张利兵〕

物联网开发实战 -〔郭朝斌〕

高楼的性能工程实战课 -〔高楼〕

业务开发算法50讲 -〔黄清昊〕

网络排查案例课 -〔杨胜辉〕

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