日期:2014-05-16  浏览次数:20356 次

JQuery 事件bind传入this
var dhtml =
{
  a1 : "a1",
  a2 : "a2",

  init : function()
  {
  $("div").bind('click', function() {
  this.a1 = "a2";
  });
  }
};


这段代码里this是指div不是我想要的dhtml.a1,请问有什么办法可以传入this?

------解决方案--------------------
TRY
var dhtml =
{
a1 : "a1",
a2 : "a2",

init : function()
{
var that = this;
$("div").bind('click', function() {
that.a1 = "a2";
});
}
};
------解决方案--------------------
在复杂的大量代码里,如果自己不能确定this关系的时候,直接用对象名称也是个不错的选择!
init : function()
{
$("div").bind('click', function() {
dhtml.a1 = "a2"; //这里直接用对象名
});
}