Luc*_*one 1 javascript jquery function-call method-call
是否可以使用call methodjavascript(如[mdn documentation]中所述)来传递参数this?
有这样的代码:
console.log(this);
$('#image_id').load(function () {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
我希望第二个this(包含在其中的那个load function)与第一个相同.
我试过了
console.log(this);
$('#image_id').load.call(this, function () {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
提前感谢大家的任何建议.
你不需要call(或apply)
你应该这样做:
var that = this;
console.log(this);
$('#image_id').load( function () {
console.log(that);
});
Run Code Online (Sandbox Code Playgroud)
Javascript具有词法作用域,这意味着该变量that可用于您的回调,并具有定义它的值.在这种情况下,that被定义为this在外部范围内.
| 归档时间: |
|
| 查看次数: |
3128 次 |
| 最近记录: |