HTML
英 [ˌeɪtʃ ti: em ˈel] 美 [ˌetʃtiɛmˈɛl]
abbr.Hypertext Markup Language 超文本链接标示语言
jquery html()方法 语法
作用:html() 方法返回或设置被选元素的内容 (inner HTML)。如果该方法未设置参数,则返回被选元素的当前内容。
返回元素内容:当使用该方法返回一个值时,它会返回第一个匹配元素的内容。
语法:$(selector).html()
设置元素内容:当使用该方法设置一个值时,它会覆盖所有匹配元素的内容。
语法:$(selector).html(content)
参数:
| 参数 | 描述 |
| content | 可选。规定被选元素的新内容。该参数可包含 HTML 标签。 |
使用函数设置元素内容:使用函数来设置所有匹配元素的内容。
语法:$(selector).html(function(index,oldcontent))
参数:
| 参数 | 描述 |
| function(index,oldcontent) | 规定一个返回被选元素的新内容的函数。 |
| index | 可选。接收选择器的 index 位置。 |
| oldcontent | 可选。接收选择器的当前内容。 |
jquery html()方法 示例
<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
$("p").html("Hello <b>world!</b>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class="btn1">改变 p 元素的内容</button>
</body>
</html>点击 "运行实例" 按钮查看在线实例