jQuery的绑定事件触发事件 [英] jQuery Bind event firing the event

查看:221
本文介绍了jQuery的绑定事件触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话的第一个函数下方的第二个功能绑定到onclick事件。

但奇怪的是,调用射击第二个函数的第一个函数的结果。

在第一个函数的参数LINKNAME是表TD元素的名称 - 可能不相关

 函数EnableExpand(LINKNAME,I){
    $(链接名称).addClass(链接文本);
    $(链接名称).bind(onclick事件,ExpandFrame(LINKNAME,I));
}功能ExpandFrame(LNAME,I){
    $(#mapFrame+ I).attr({身高:500,宽度:800});
}


解决方案

的问题是,第二个参数.bind()应该是一个函数,但你调用一个函数。 JQuery的评估函数调用,并试图将结果click事件绑定。

通过包装的匿名函数的函数调用,如建议,JavaScript引擎将绑定包装功能的事件,而不是评估里面是什么了。

  $(链接名称)。点击(函数(){ExpandFrame(LINKNAME,I)});

它可能有助于认识到使用以下等同于上方的code

  $(链接名称)。点击(函数(){ExpandFrame(LINKNAME,I)}());

I'm calling the first function below to bind the second function to the onClick event.

The strange thing is that calling the first function results in firing the second function.

The LinkName parameter in the first function is a name of a table td element - probably not relevant.

function EnableExpand(LinkName, i) {
    $(LinkName).addClass("link-text");
    $(LinkName).bind("onclick", ExpandFrame(LinkName, i));
}

function ExpandFrame(lName, i) {
    $("#mapFrame" + i).attr({ height: 500, width: 800 });
}

解决方案

The problem is that the second argument to .bind() should be a function, but you are calling a function. JQuery evaluates the function call and tries to bind the result to the click event.

By wrapping the function call in a anonymous function, as suggested, the javascript engine will bind the wrapper function to the event, and not evaluate what's inside it.

$(LinkName).click( function () { ExpandFrame(LinkName, i) } );

It may be helpful to realize that using the following is equivalent to your code above.

$(LinkName).click( function () { ExpandFrame(LinkName, i) }() );

这篇关于jQuery的绑定事件触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆