jQuery 中的 html( val )方法函数

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

html(val)方法设置每个匹配元素的html内容。此属性在XML文档上不可用,但适用于XHTML文档。

html( val ) - 语法

selector.html( val )

这是此方法使用的所有参数的描述-

  • val  -  任何字符串

html( val ) - 示例

以下示例将获取第一段的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").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.

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

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

Java性能调优实战 -〔刘超〕

职场求生攻略 -〔臧萌〕

容器实战高手课 -〔李程远〕

Python自动化办公实战课 -〔尹会生〕

操作系统实战45讲 -〔彭东〕

陈天 · Rust 编程第一课 -〔陈天〕

搞定音频技术 -〔冯建元 〕

快速上手C++数据结构与算法 -〔王健伟〕

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