我想知道是否有人知道 .load() 方法对从 url 检索到的数据到底做了什么? 它是否用检索到的数据替换目标选择器内容? 或者将该数据附加到目标选择器?
不过,在我看来,.load() 方法会替换(覆盖)目标元素的内容...
文档有点模糊:
Description: Load data from the server and place the returned HTML into the matched element.
或者
.load() sets the HTML contents of the matched element to the returned data.
最佳答案
它替换内容。同意the documentation可能会更清楚,尽管如果它是附加的,我希望它是明确的。
本质上,忽略一些细节,是这样的:
$("selector").load(url);
实际上是这样的:
$.get(url, function(html) {
$("selector").html(html);
});
如果您告诉 jQuery 您只想加载返回的 HTML 的片段,那就有点复杂了。
关于javascript - jQuery:.load() 方法替换目标选择器内容或附加到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993132/