我在做项目时遇到问题。基本上,我在另一个 HTML 中有很多对话框div
,并且必须加载所需的对话框并将其附加到我的包装器div
。
如何通过 id 加载元素并附加到div ?我已经搜索了我的答案,我想出了使用$.ajax()
但它无法加载单个元素(或者我可能做错了什么)。
.JS:
$.ajax({
url: "dialogs.html#modal_" + theRestOfItsId,
async: false
}).done(function(html) {
$("#dialog-area").prepend(html);
});
其中#dialog-area
是对话框包装器(包含所有需要的对话框的包装器,因此我可以轻松清理它)
虽然我很确定它不起作用,但这就是我想做的,而不是使用 $("#dialog-area").load("dialogs.html#modal_" + theRestOfItsId)
.
你可以采用.load()
的实现并使其像这样工作:
$.ajax({
url: "dialogs.html",
dataType: 'html',
}).done(function(html) {
var selector = '#modal_' + theRestOfItsId;
$('<div>')
.append($.parseHTML(html))
.find(selector)
.prependTo('#dialog-area');
});
尝试修改代码,如下所示.html.d
$.ajax({
url: "dialogs.html #modal_" + theRestOfItsId,
async: false
}).done(function(html) {
$("#dialog-area").prepend(html.d);
});
我在等待时做出了自己的答案。
修复:
$("body").append("<div id="temp"></div>");
$("#temp").load('dialogs.html #modal_'+theRestOfItsId, "" , function(){
var temp = $("#temp").html();
$(temp).prependTo("#dialog-area");
$("#temp").remove();
});