load() API调用函数回调范围
我已经用 JS 编码有一段时间了,但不知怎的,我总是遇到一些“抓头”类型的问题。我认为我需要进行一些深入研究,但与此同时有人可以帮助解决这个问题吗?
所以问题是这样的: 我正在将项目(由 $(this) 获取)传递到回调函数中。这段代码不起作用——当我真的认为它应该起作用时。由于我已将 $(this) 放入变量中(克隆数据?),然后通过函数将其传递到回调中,所以它肯定不会丢失数据吗?但确实如此,而且可怕的是
// Allow hrefs with the class 'ajax' and a rel attribute set with a selector to load via ajax into that selector.
$(".ajax").unbind("click").click
(
function(e)
{
var locationhint = $(this).attr("rel");
var $location = $(locationhint);
$location.html ("<img src='images/blockloading.gif' />");
$location.load($(this).attr("href").replace("index.php", "ajax.php"), '', function($location){dready($location);});
e.preventDefault();
}
);
,现在不起作用。
这个有效:
$(".ajax").unbind("click").click
(
function(e)
{
$("#myspecificdiv").load($(this).attr("href").replace("index.php", "ajax.php"), '', function(){dready($("#myspecificdiv"));});
e.preventDefault();
}
);
我认为这是一个范围问题,但我也做了这个,它应该有效,因为它与上面的“静态”完全一样,本质上,因为它传递元素的文本 ID。 这个也坏了:。
$(".ajax").unbind("click").click
(
function(e)
{
var locationhint = $(this).attr("rel");
var $location = $(locationhint);
$location.html ("<img src='images/blockloading.gif' />");
var locationid = "#" + $location.attr("id");
$location.load($(this).attr("href").replace("index.php", "ajax.php"), '', function(locationid){dready($(locationid));});
e.preventDefault();
}
);
损坏的,当我console.log locationid时,返回目标DIV的内部HTML。 $location.attr("id");当其他地方没有调用它时,无法返回原始 HTML?那么这是我接受的范围吗?但那么它是如何获取内部 HTML 的呢?
有什么解决办法吗?
更新: 在我发布这篇文章 12 秒后,我突然想到 function($location){dready($location);} 回调上的内部函数可能会自动传递 AJAX 调用响应?但为什么?
I have been coding in JS for a while now, but somehow I always get a bit of a 'scratch head' type of problem. I think that I need to do some digging in, but in the meanwhile can someone help with this one?
So here is the problem:
I am passing through the item (got by $(this)) into the callback function. This code does not work - when I really think it should. Since I have placed the $(this) into a variable (cloning the data?) and then passed it into the callback through a function, surely it should not loose the data? But it does, and horribly
// Allow hrefs with the class 'ajax' and a rel attribute set with a selector to load via ajax into that selector.
$(".ajax").unbind("click").click
(
function(e)
{
var locationhint = $(this).attr("rel");
var $location = $(locationhint);
$location.html ("<img src='images/blockloading.gif' />");
$location.load($(this).attr("href").replace("index.php", "ajax.php"), '', function($location){dready($location);});
e.preventDefault();
}
);
Now that doesn't work.
This one works:
$(".ajax").unbind("click").click
(
function(e)
{
$("#myspecificdiv").load($(this).attr("href").replace("index.php", "ajax.php"), '', function(){dready($("#myspecificdiv"));});
e.preventDefault();
}
);
I take it it's a scope problem, but I have also done this, which should work because it's exactly the same as the 'static' one above, essentially, because it's passing the text ID of the element. This one also breaks:.
$(".ajax").unbind("click").click
(
function(e)
{
var locationhint = $(this).attr("rel");
var $location = $(locationhint);
$location.html ("<img src='images/blockloading.gif' />");
var locationid = "#" + $location.attr("id");
$location.load($(this).attr("href").replace("index.php", "ajax.php"), '', function(locationid){dready($(locationid));});
e.preventDefault();
}
);
The broken ones, when I console.log locationid, return the internal HTML of the target DIV. $location.attr("id"); can't return raw HTML when there is no call to it anywhere else surely? So it's scope I take it? But then how is it getting the internal HTML to spit out?
Any solutions?
Update:
12 seconds after I posted this it occurred to me that function($location){dready($location);} the internal function on the callback might automagically pass the AJAX call response through? But why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能执行此
function($location){dready($location);}
但您不需要这样做,因为外部作用域已定义 $location ,回调函数将理解此 var 而无需尝试传递它到回调函数中,因此在这种情况下function(){dready($location);}
将起作用。
解决范围问题的另一种方法是使用 $.proxy。这允许您设置调用函数的 this 上下文。 jQuery.proxy( dready, $location ) 返回一个可用作回调的函数,调用该函数时将调用 dready 方法,并将其设置为 $location。
例如
You cannot do this
function($location){dready($location);}
but you do not need to since the outer scope has $location defined the callback func will understand this var without trying to pass it into the callback funcion so in this instancefunction(){dready($location);}
will work.
Another way to conquor scoping issues is to use $.proxy. This allows you to set the this context under which the function will be called.
jQuery.proxy( dready, $location )
returns a function that can be used as a callback which when invoked will call the method dready with this set to to $location.e.g