当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery append()方法教程与html()方法教程的区别及使用介绍

jquery append()方法教程与html()方法教程的区别及使用介绍

2019年03月25日  | 移动技术网IT编程  | 我要评论

append(content):方法在被选元素的结尾(仍然在内部)插入指定内容,有很多朋友觉得append与html差不多,其它从英文意义上append是在原有基础上增加,而html中是替换当前所有内容。

定义和用法

append() 方法在被选元素的结尾(仍然在内部)插入指定内容。
$(selector).append(content)

使用函数来附加内容
使用函数在指定元素的结尾插入内容。

语法

$(selector).append(function(index,html))

实例代码:

<script src="/jquery.min.js" type="text/javascript"></script> 
<style> 
.imgfocus{border: 1px solid #eee;} 
</style> 
<p> </p> 
<script type="text/javascript"> 
var showimg = "<p class='imgfocus'>123456</p>"; 
$("p").append(showimg); 
</script>

html() 方法返回或设置被选元素的内容 (inner html)。

如果该方法未设置参数,则返回被选元素的当前内容。
返回元素内容
当使用该方法返回一个值时,它会返回第一个匹配元素的内容。

语法

$(selector).html()

设置所有 p 元素的内容:

$(".btn1").click(function(){ 
$("p").html("hello <b>world</b>!"); 
});

指定元素中清空

$("a[href$='logout.asp']").click(function(event) { 
event.preventdefault(); 
$.get("/xxlr/logout.asp","",function(data, textstatus) { 
if (data == 1) { //表明注销成功 
$('#message').html(""); 
$("#userlogin>p").show(); 
} 
else { 
$('#message').append("<p><strong>注销失败,请重新尝试!</strong></p>"); 
} 
}); 
});


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网