如何模拟追加回调函数? [英] How to simulate an append callback function?

查看:26
本文介绍了如何模拟追加回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 .append() 没有任何回调函数,但是当我尝试这样做时:

I know that .append() does not have any callback function, but when I try to do this:

$.onShadowboxFinished = function (f) {

var postid = $(f.link).parent().attr("id");
var loadUrl = "wp-admin/admin-ajax.php?action=mfields_show_post&p=" + postid;
$('#infos_wrap').load(loadUrl).append();
alert("loaded! test selector :"+$('a.projectimgs').first().attr("href"));

}

我正在将内容(链接和图像)加载到 div #infos_wrap.加载所有链接和图像后,我想操作内容(例如:选择链接以将它们添加到 shadowbox 缓存)

I am loading content ( links and images ) into the div #infos_wrap. Once all links and images are loaded, I want to manipulate content ( example : Select the links to add them to the shadowbox cache )

目前,第 4 行在第 3 行之前执行.

For now, the 4th line is executed before the 3th.

感谢您的帮助!

PS:如果有人真的想帮忙(我会很感激^^)
您可以查看网站:http://www.tom-portfolio.fr/wp-login.php
登录:访客
通行证:访客
javascript 文件是 portfolio.js
使用您的萤火虫或 GoogleDev 工具查看它!
非常感谢!

PS : If someone want really to help ( i'd appreciate it ^^ )
You can check the site : http://www.tom-portfolio.fr/wp-login.php
login: visitor
pass: visitor
The javascript file is portfolio.js
Use your firebug or GoogleDev tools to view it!
Thanks a lot !

推荐答案

你要做的是使用 .load() 函数的回调函数,因为它是异步的(意思是后面的代码它会一直执行,直到收到响应并触发其回调函数):

What you want to do is use the callback function for the .load() function because it is asynchronous (meaning the code after it keeps executing until it recieves a response and fires its callback function):

var postid  = $(f.link).parent()[0].id,
    loadUrl = "wp-admin/admin-ajax.php?action=mfields_show_post&p=" + postid;

$('#infos_wrap').load(loadUrl, function () {
    alert("loaded! test selector :"+$('a.projectimgs').first()[0].href);
});

请注意,.load() 会将 #infos_wrap 的内容替换为来自 AJAX 调用的响应(在本例中为 #infos_wrap) 所以我们不必使用 .append() (在内部使用 .html() 替换所选元素的 HTML 而不是附加回应).

Note that .load() will replace the contents of #infos_wrap with the response from the AJAX call (#infos_wrap in this case) so we don't have to use .append() (internally .html() is used which replaces the HTML of the selected element(s) rather than appending the response).

另请注意,我在不需要的地方更改了 .attr() 的几个实例.

Also note that I changed a couple instances of .attr() where it wasn't needed.

.load() 的文档:http://api.jquery.com/加载

这篇关于如何模拟追加回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆