- 0133技术站
- 联系QQ:18840023
- QQ交流群

- 微信公众号

jQuery html() 方法
定义和用法
html() 方法设置或返回被选元素的内容(innerHTML)。
当该方法用于返回内容时,则返回第一个匹配元素的内容。
当该方法用于设置内容时,则重写所有匹配元素的内容。
提示:如只需设置或返回被选元素的文本内容,请使用 text() 方法。
语法
返回内容:
$(selector).html()
设置内容:
$(selector).html(content)
使用函数设置内容:
$(selector).html(function(index,currentcontent))
参数 | 描述 |
content | 必需。规定被选元素的新内容(可包含 HTML 标签)。 |
function(index,currentcontent) | 可选。规定返回被选元素的新内容的函数 index : 返回集合中元素的 index 位置 currentcontent : 返回被选元素的当前 HTML 内容。 |
<script> $(document).ready(function(){ $("button").click(function(){ $("p").html("Hello <b>world!</b>"); }); }); </script> </head> <body> <button>修改所有P元素的内容</button> <p>这是一个段落。</p> <p>这是另一个段落。</p>
点击 "运行实例" 按钮查看在线实例
效果图:
推荐手册