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.

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

技术教程推荐

程序员进阶攻略 -〔胡峰〕

重学前端 -〔程劭非(winter)〕

职场求生攻略 -〔臧萌〕

基于人因的用户体验设计课 -〔刘石〕

爆款文案修炼手册 -〔乐剑峰〕

Serverless进阶实战课 -〔静远〕

Go进阶 · 分布式爬虫实战 -〔郑建勋〕

零基础学Python(2023版) -〔尹会生〕

云时代的JVM原理与实战 -〔康杨〕

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