使用jquery或javascript查找调用事件的按钮对象 [英] Find the button object that called the event with jquery or javascript

查看:102
本文介绍了使用jquery或javascript查找调用事件的按钮对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using jquery to bind an event to a few buttons...

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

在函数stageButtonClick()中,我想获取调用按钮的对象,但是我不能在stageButtonClick函数内使用$(this),它对我没有任何回报.

In the function stageButtonClick(), I want to get the object of the calling button, but I cannot use $(this) inside the stageButtonClick function, it doesn't return anything for me.

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

Also, please don't suggest the inline function usage

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

我想学习这种方法.

推荐答案

您的问题在这里:

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

分配命名函数时请勿使用括号,因为它将调用该函数并分配结果.

Don't use parenthesis when assigning a named function because it will call the function and assign the result instead.

这有效:

演示

Demo

$(document).ready(function(){

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

});

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

这篇关于使用jquery或javascript查找调用事件的按钮对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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