本文摘自PHP中文网,作者青灯夜游,侵删。
html()方法用于返回或设置被选元素的内容:1、如果未设置参数,则返回被选元素的当前内容,语法“$(selector).html()”;2、如果设置参数,则会覆盖所有匹配元素的内容,语法“$(selector).html(内容值)”。
本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
jQuery html()方法
html() 方法返回或设置被选元素的内容 (inner HTML)。
当使用该方法返回一个值时,它会返回第一个匹配元素的内容。
语法
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< script src = "https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" >
</ script >
< script >
$(document).ready(function() {
$("button").click(function() {
alert($("p").html());
});
});
</ script >
</ head >
< body >
< p >这是一个 < b >段落</ b >。</ p >
< button >返回P标签的内容</ button >
</ body >
</ html >
|
当使用该方法设置一个值时,它会覆盖所有匹配元素的内容。
语法
1 | $(selector).html(content)
|
content:规定被选元素的新内容,可省略。该参数可包含 HTML 标签。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< script src = "https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" >
</ script >
< script >
$(document).ready(function() {
$("button").click(function() {
$("p").html("Hello < b >world!</ b >");
});
});
</ script >
</ head >
< body >
< p >这是一个段落。</ p >
< p >这是另一个段落。</ p >
< button >修改所有P元素的内容</ button >
</ body >
</ html >
|
相关视频教程推荐:jQuery教程(视频)
以上就是jQuery html()方法有什么用的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
jQuery版本几适应ie8
jQuery三大版本之间有什么区别?
jQuery如何添加节点?
jQuery的ajax()函数传值中文乱码怎么办
jQuery中的webix是什么
jQuery为什么不支持live
jQuery的选择器有几种
jQuery 如何判断是否有div
jQuery中$符号的作用是什么?
jQuery怎样判断是否是字符串
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jQuery html()方法有什么用