jquery.html有效,但是.add不行;t

jquery .html works but .add doesn't

本文关键字:不行 add 但是 html 有效 jquery      更新时间:2023-09-26

我的代码:

$(document).ready(function() {
    $(this.body).html("<p>using .html</p>"); // works
    $(this.body).add("<p>using .add</p>"); // doesn't work
});​

一些jsfiddle

我做错了什么?

您想要使用append:

$(this.body).append("<p>using .add</p>")

add代表其他东西:

描述:将元素添加到匹配的元素集中。

您应该使用append而不是add。小提琴

add()不写入DOM,它向jQuery对象添加了更多元素。

var x = $("li") //would create a jQuery object
x.add("span") //would add span tags to that object. 

要编写要append()或appendTo()的jQuery对象的内容。。。

x.appendTo("body");

add描述:"Add elements to the set of matched elements."参考

html将取代this.body内容

append将添加到内容的末尾。也许这就是你想要的

$(this.body).append('<p>using .append</p>');