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.
参考链接
https://www.learnfk.com/jquery/attr-html.html