jquery Ajax:选择器作为函数参数没有被记录?
我不知道为什么这对我不起作用。
我在链接上调用这个!
$('.pagination a').live('click', function(e) {
e.preventDefault();
var page = $(this).attr("data-ajax-link");
//get classname of container box that should be reloaded
var box = $(this).parent().parent();
ajaxLoad(box, page, false);
console.log(box); //takes some time as well! WHY?
});
这里是被触发的函数。
function ajaxLoad(targetBox, loadUrl) {
console.log('start');
console.log($(targetBox));
$.ajax({
url: loadUrl,
dataType: "html",
success: function(html,status) {
console.log('end');
我没有得到的奇怪的事情是 console.log('start')
立即记录在我的控制台中,但 console.log($(targetBox));
是直到ajax函数成功返回后才出现在控制台中!
这是为什么?有什么想法吗?
这是触发该函数的 HTML。
<div class="list wide">
<div class="pagination">
<span><</span>
<em>[1]</em>
<a data-ajax-link="/ajax/render/project/my?page=2" rel="next" href="/my/project/2">2</a>
<a data-ajax-link="/ajax/render/project/my?page=2" rel="next" href="/my/project/2">></a>
</div>
...
所以我用parent().parent()选择的元素是div.list。因此,实际上 .list 应在单击链接时立即记录。
I don't have a clue why this does not work for me.
I'm calling a this on a linkclick!
$('.pagination a').live('click', function(e) {
e.preventDefault();
var page = $(this).attr("data-ajax-link");
//get classname of container box that should be reloaded
var box = $(this).parent().parent();
ajaxLoad(box, page, false);
console.log(box); //takes some time as well! WHY?
});
And here the function that's triggered.
function ajaxLoad(targetBox, loadUrl) {
console.log('start');
console.log($(targetBox));
$.ajax({
url: loadUrl,
dataType: "html",
success: function(html,status) {
console.log('end');
The weird thing I don't get is that console.log('start')
is logged immediately in my console but console.log($(targetBox));
is not appear in the console until the success of the ajax function comes back!
why is that? Any idea?
This is the HTML that that is triggering the function.
<div class="list wide">
<div class="pagination">
<span><</span>
<em>[1]</em>
<a data-ajax-link="/ajax/render/project/my?page=2" rel="next" href="/my/project/2">2</a>
<a data-ajax-link="/ajax/render/project/my?page=2" rel="next" href="/my/project/2">></a>
</div>
...
So the element I'm selecting with parent().parent() is the div.list. So actually .list should be logged immediately when clicking the link.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这个简单的示例中适用于我:
http://jsfiddle.net/vmkcG/
您可以更新以显示这的背景?也就是说,在用于分配给框的函数上下文中显示它?
Works for me in this simple example:
http://jsfiddle.net/vmkcG/
Can you update to show the context of this? That is, show this in the context of the function where it's being used to assign to box?