[ jquery 方法 html([val|fn]) ] 此方法操作匹配的元素或元素集中的内容,相当于javascript中的innerHTML

取得第一个匹配元素的html内容,这个函数不能用于XML文档,但可以用于XHTML文档,在一个 HTML 文档中, 我们可以使用 .html() 方法来获取任意一个元素的内容。 如果选择器匹配多于一个的元素,那么只有第一个匹配元素的 HTML 内容会被获取,相当于javascript中的innerHTML,可读可写,可以解析HTML标签

如果传入的是callback,此函数返回一个字符串并且参数解析如下:

  1.index为元素在集合中的索引位置

  2.html为原先的HTML值。

实例:

<!DOCTYPE html>
<html lang='zh-cn'>
<head>
<title>Insert you title</title>
<meta http-equiv='description' content='this is my page'>
<meta http-equiv='keywords' content='keyword1,keyword2,keyword3'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- <script type='text/javascript' src='./js/jquery-3.0.0.js'></script> -->
<script type='text/javascript' src='./js/jquery-1.12.1.min.js'></script>
<style type='text/css'>
   div{font:400 25px/250px 'Courier New';text-align:center;color:#000;border:1px solid #F50;700px;height:250px;margin:20px auto;}
   .addOne{padding:0 8px;}
   .addTwo{color:#999;}
</style>
<script type='text/javascript'>
    $(function(){
            alert($('#user').html());
           $('#user').html('<strong>this is a test</strong>');
    });
</script>
</head>
<body>
   <div id='user' class='addOne addTwo'>oldChar</div>
</body>
</html>
原文地址:https://www.cnblogs.com/mysearchblog/p/5605566.html