我正在使用jQuery将事件绑定到几个按钮...

$(".numField").bind("click", stageButtonClick());


在函数stageButtonClick()中,我想获取调用按钮的对象,但是我不能在$(this)函数内使用stageButtonClick,它不会为我返回任何内容。

另外,请不要建议内联函数的用法

$(".numField").bind("click", function() {...})


我想学习如何用这种方法。

最佳答案

您的问题在这里:

$(".numField").bind("click", stageButtonClick());
                       don't use parenthesis ^^


分配命名函数时不要使用括号,因为它将调用该函数并分配结果。

这有效:

Demo

$(document).ready(function(){

    $(".numField").bind("click", stageButtonClick);

});

function stageButtonClick()
{
     alert( $(this).attr("id") );
}

关于javascript - 使用jquery或javascript查找调用事件的按钮对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13887252/

10-12 14:10