我得到的 id 是未定义的。 http://jsfiddle.net/valamas/YUPWu/
我希望有人能接受我的(微不足道的?)错误。
谢谢
这是因为 $(this) 什么都没有?$(this) 通常表示您选择的项目.. 在您的情况下它什么都不是,因为在该函数内部它没有指向任何元素。你可以这样做
$(function (){
$(document).on('click', "#MyId", function () {
var theId = $(this).prop('id'); //$(this).id does not work either.
alert(theId);
});
});
是因为this
无法访问。演示
$(function ()
{
$(document).on('click', "#MyId", function () { MyId_Click(this); });
});
function MyId_Click(obj)
{
var theId = $(obj).attr('id');
alert(theId);
}