Jquery.click()获取此
在我的类的方法中,我有:Jquery.click()获取此,jquery,click,this,Jquery,Click,This,在我的类的方法中,我有: this.background.click(function() { this.terminate(); }); 显然this.terminate()不起作用,因为此在jquery中引用了此背景。请单击函数。如何从上级类中获取this?声明一个包含外部this的变量: var self = this; this.background.click(function() { self.terminate(); }); 尝试: 您可以像@Andrew sa
this.background.click(function() {
this.terminate();
});
显然
this.terminate()代码>不起作用,因为此
在jquery中引用了此背景
。请单击
函数。如何从上级类中获取this
?声明一个包含外部this
的变量:
var self = this;
this.background.click(function() {
self.terminate();
});
尝试:
您可以像@Andrew said一样制作,也可以制作“yourObject.terminate();”
例:
为了提高性能,请确保“后台”选择器存在(如果javascript在html之前加载),并使用jquery“live()”或“delegate()”或“on()”在元素中附加事件。使用bind
$(".detailsbox").click(function(evt){
test.bind($(this))();
});
function test()
{
var $this = $(this);
}
var yourObject = {
background: $("#background"),
terminate: function ()
{
alert("do something...");
},
addEvents: function ()
{
this.background.click(function()
{
yourObject.terminate();
});
}
};
$(function()
{
yourObject.addEvents();
});
$(".detailsbox").click(function(evt){
test.bind($(this))();
});
function test()
{
var $this = $(this);
}