Javascript Jquery-如何访问单击以运行函数的元素?

Javascript Jquery-如何访问单击以运行函数的元素?,javascript,jquery,html,events,Javascript,Jquery,Html,Events,我正在使用jQuery。我有一些代码如下- ----HTML----- 如何访问导致函数运行的元素?例如,在上面的代码中,我想访问从函数do_Something()内部单击的单元格的id 当然,直接称呼它会更简单: $(".cell").click(do_something); function do_something(){ console.log(this.id); // open the console to see the result } 或 当然,直接称呼它会更简单:

我正在使用jQuery。我有一些代码如下-

----HTML-----

如何访问导致函数运行的元素?例如,在上面的代码中,我想访问从函数do_Something()内部单击的单元格的id

当然,直接称呼它会更简单:

$(".cell").click(do_something);  
function do_something(){
    console.log(this.id); // open the console to see the result
}

当然,直接称呼它会更简单:

$(".cell").click(do_something);  
function do_something(){
    console.log(this.id); // open the console to see the result
}


他想显示id而不是值,所以试试这个:$(this).attr('id')
this.id
更好…他想显示id而不是值,所以试试这个:$(this.attr('id')
this.id
更好…谢谢!!我不能把函数放在里面,因为我需要在代码中的很多地方这样做-所以不必要的重复代码。谢谢!!我不能把函数放在里面,因为我需要在代码中的很多地方这样做,所以不必要地重复代码。
$(".cell").click(function() {
    do_something(this); // this is the clicked element
});
function do_something(element){
    console.log(element.id); // open the console to see the result
}
$(".cell").click(do_something);  
function do_something(){
    console.log(this.id); // open the console to see the result
}
$(".cell").click(function(){
    console.log(this.id); // open the console to see the result
});