为什么我不能使用jQuery获取此按钮的ID?

ben*_*ben 2 jquery

的jsfiddle

我正在尝试使用按钮来获取ID $(this).id,但它是未定义的.我究竟做错了什么?谢谢阅读.

编辑:来自jsFiddle的代码示例:

HTML

<button id='remove_button' type='button'>Remove</button>?
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('#remove_button').mouseup(function(){
     alert($(this).id);
});?
Run Code Online (Sandbox Code Playgroud)

dan*_*ana 8

$(this)

为您提供对jQuery对象的引用.您可以使用像Mario所说的attr()函数,或者甚至只是这样做:

this.id

  • +1绝对使用这种方法.将`this`包装在jQuery对象中并调用方法来访问一个就在那里的属性是愚蠢的. (2认同)