html()和text()的区别

在获取内容时:

html()会将其中的其他标签提取出来
text()会忽略其中的标签,只显示内容文字
例:
在这里插入图片描述
1.使用html()获取span标签中的内容

$(".minus").on("click",function(){
        var num = $(this).next().html();
        console.log(num)
})

运行结果:
在这里插入图片描述

2.使用text()获取span标签中的内容

$(".minus").on("click",function(){
        var num = $(this).next().text();
        console.log(num)
})

运行结果:2

在设置 / 更改元素内容时:

html()在更改内容时,可以加入标签
text()更改的是文字内容

例:
1.使用html()设置内容

$("p").html("111");

运行结果:加粗后的111

2.使用text()设置内容

$("p").text("111")

运行结果:111

你可能感兴趣的:(javascript,html)