使用 jQuery 动态添加 div 时获取最新的 div 列表
在将动态添加的 DIV 添加到 DOM 后立即访问它们时遇到一些问题。
我有一个大致类似的函数,它接受输入,获取 JSON 数据并创建一个 div。
function addAttrib(attrib) {
$.getJSON("file.json", function(data) {
//Do all the stuff
var newDiv = $("<div class='word'>"+label+"</div>").insertAfter("#mapLabels");
var adjustedX = x - (newDiv.width()/2);
var adjustedY = y - (newDiv.height()/2);
newDiv.css("left",adjustedX);
newDiv.css("top",adjustedY);
newDiv.fadeIn("slow");
};
saveAttribs()
};
然后我有第二个函数,基本上我只想创建“Word”类页面上所有 DIV 的数组。 (这样我以后就可以将每个新的 div 保存到单独的 JSON 文件中)。
因此,在上面的函数末尾调用的第二个函数称为“Save Attribs”,现在我只是处于调试模式,因此使用 console.log。
function saveAttribs() {
$.wordTracker.maps[$.wordTracker.currentMap] = [];
$.wordTracker.maps[$.wordTracker.currentMap].length = 0;
$(".word").each( function() {
//items.push($(this).text());
$.wordTracker.maps[$.wordTracker.currentMap].push($(this).text());
});
console.log($.wordTracker.maps[$.wordTracker.currentMap]);
}
问题是,当它写入控制台时,它总是落后 1 个 div。因此,在我添加第一个之后,它什么也不返回,第二个返回第一个,第三个返回第一个和第二个,依此类推。
我该怎么做才能确保它获取最新的 DIV 列表 - 或者等到它们完全加载?
根据此处的响应,这是第一个函数的更完整版本。
function addAttrib(attrib) {
$.getJSON("file.json", function(data) {
//Cut out some vars for simplicity...
var exists = $('#'+attrib).length;
if (exists >= 1) {
$('#'+attrib).fadeOut("fast", function() { $(this).remove()} );
//$('.word').remove();
} else {
var newDiv = $("<div class='word' zone='"+data[attrib].ZoneName+"' map='"+$.wordTracker.currentMap+"' id='"+attrib+"'>"+label+"</div>").insertAfter("#mapLabels");
var adjustedX = x - (newDiv.width()/2);
var adjustedY = y - (newDiv.height()/2);
newDiv.css("left",adjustedX);
newDiv.css("top",adjustedY);
newDiv.fadeIn("slow");
}
focusOnInput();
saveAttribs();
});
}
Having a bit of an issue accessing dynamically added DIV's immediately after they've been added to the DOM.
I have a function roughly like this that takes an input, get's the JSON data and creates a div.
function addAttrib(attrib) {
$.getJSON("file.json", function(data) {
//Do all the stuff
var newDiv = $("<div class='word'>"+label+"</div>").insertAfter("#mapLabels");
var adjustedX = x - (newDiv.width()/2);
var adjustedY = y - (newDiv.height()/2);
newDiv.css("left",adjustedX);
newDiv.css("top",adjustedY);
newDiv.fadeIn("slow");
};
saveAttribs()
};
Then I have a second function which basically I just want to create an array of all the DIV's on the page of class "Word". (It is so I can save each new div to a separate JSON file later).
So the second function which is called at the end of the one above is called Save Attribs, and for now I'm just in debug mode, so using console.log.
function saveAttribs() {
$.wordTracker.maps[$.wordTracker.currentMap] = [];
$.wordTracker.maps[$.wordTracker.currentMap].length = 0;
$(".word").each( function() {
//items.push($(this).text());
$.wordTracker.maps[$.wordTracker.currentMap].push($(this).text());
});
console.log($.wordTracker.maps[$.wordTracker.currentMap]);
}
The problem is that when it write to the console it is always 1 div behind. So after I add the first, it returns nothing, the second returns the first, the third returns the first and second and so on.
What can I do to make sure it grabs the most up do date list of DIV's - or waits until they are fully loaded?
Based on response here is a more complete version of the first function.
function addAttrib(attrib) {
$.getJSON("file.json", function(data) {
//Cut out some vars for simplicity...
var exists = $('#'+attrib).length;
if (exists >= 1) {
$('#'+attrib).fadeOut("fast", function() { $(this).remove()} );
//$('.word').remove();
} else {
var newDiv = $("<div class='word' zone='"+data[attrib].ZoneName+"' map='"+$.wordTracker.currentMap+"' id='"+attrib+"'>"+label+"</div>").insertAfter("#mapLabels");
var adjustedX = x - (newDiv.width()/2);
var adjustedY = y - (newDiv.height()/2);
newDiv.css("left",adjustedX);
newDiv.css("top",adjustedY);
newDiv.fadeIn("slow");
}
focusOnInput();
saveAttribs();
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在插入之后调用它,因此在
$ 内。 getJSON()
回调,如下所示:回调函数稍后在请求完成时运行,因此当前您的
saveAttribs()
在You need to call it after the insert, so inside the
$.getJSON()
callback, like this:The callback functions runs later when the request completes, so currently your
saveAttribs()
runs before the<div>
is actually added...by making the call in the callback after the append like above will give you the latest elements.