我的代碼:jQuery的html的作品,但。新增不
$(document).ready(function() {
$(this.body).html("<p>using .html</p>"); // works
$(this.body).add("<p>using .add</p>"); // doesn't work
});
我在做什麼錯?
我的代碼:jQuery的html的作品,但。新增不
$(document).ready(function() {
$(this.body).html("<p>using .html</p>"); // works
$(this.body).add("<p>using .add</p>"); // doesn't work
});
我在做什麼錯?
您應該使用追加而不是添加。 fiddle
add()不會寫入DOM,它會向jQuery對象添加更多元素。
var x = $("li") //would create a jQuery object
x.add("span") //would add span tags to that object.
要寫jQuery對象的內容要追加()或appendTo()...
x.appendTo("body");
add
描述:"Add elements to the set of matched elements."
reference
html
將取代this.body
內容。
append
將添加到內容的末尾。也許這就是你想要的。
$(this.body).append('<p>using .append</p>');