0

我得到的 id 是未定义的。 http://jsfiddle.net/valamas/YUPWu/

我希望有人能接受我的(微不足道的?)错误。

谢谢

4

2 回答 2

3

这是因为 $(this) 什么都没有?$(this) 通常表示您选择的项目.. 在您的情况下它什么都不是,因为在该函数内部它没有指向任何元素。你可以这样做

$(function (){
   $(document).on('click', "#MyId", function () { 
       var theId = $(this).prop('id'); //$(this).id does not work either.
       alert(theId);
   });
});

http://jsfiddle.net/YUPWu/1/

于 2012-07-04T05:50:40.177 回答
2

是因为this无法访问。演示

$(function ()
{
    $(document).on('click', "#MyId", function () { MyId_Click(this); });
});
function MyId_Click(obj)
{
    var theId = $(obj).attr('id'); 
    alert(theId);
}
​
于 2012-07-04T05:51:07.053 回答