Javascript jquery:替换onclick事件

Javascript jquery:替换onclick事件,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我有以下代码: $('#tableA').find("#expand_" + row_id).prop("onclick", null); $('#tableA').find("#expand_" + row_id).prop("onclick", 'expandAndShow('+row_id+')'); $('#tableA').find("#expand_" + row_id).removeClass('icon-plus-sign'); $('#tableA').find

我有以下代码:

  $('#tableA').find("#expand_" + row_id).prop("onclick", null);
  $('#tableA').find("#expand_" + row_id).prop("onclick", 'expandAndShow('+row_id+')');
  $('#tableA').find("#expand_" + row_id).removeClass('icon-plus-sign');
  $('#tableA').find("#expand_" + row_id).addClass('icon-ok-sign');
我想用新的方法替换以前链接的
onlick
方法。它不起作用。但是,
removeClass
addClass
运行良好。我遗漏了什么吗

这应该行得通

$('#tableA').find("#expand_" + row_id).unbind('click');
$('#tableA').find("#expand_" + row_id).on('click',expandAndShow(row_id));

要使用jQuery删除内联属性,请执行以下操作:

$('#tableA').find('#expand_' + row_id).removeAttr('onclick');
但是,对于IE<9,您应该使用:

.prop('onclick', null);
作为。
但是,我确实想知道,为什么要将
find
与ID选择器一起使用。我认为我说的
find
返回一个jQ对象数组是正确的。不是单个DOM对象。
也许:

这更合适。要用另一个属性替换
onclick
属性,您不需要2个
prop
调用,顺便说一下:

$('#expand_' + row_id).prop('onclick', 'expandAndShow('+row_id+')');
通过设置另一个处理程序,基本上删除了原始的
onclick
处理程序。总而言之,这种事情最好通过授权来完成:

$('#tableA').on('click', '*[id^=expand_]', function()
{
    alert($(this).attr('id').replace('expand_',''));
});

此代码处理
#tableA
元素的所有子元素上的所有单击事件,这些子元素具有id,以
展开
开头。然后,我继续警告该id,但不使用
expand\ucode>子字符串。

unbind用于使用jquery的事件绑定,而不是内联脚本
expandShow(row\u id)
也调用该函数,除非它返回另一个函数,否则您没有回调/handleruse
.attr()
要重新绑定内联脚本或更好,只需使用jquery绑定/取消绑定所有事件即可
$('#tableA').on('click', '*[id^=expand_]', function()
{
    alert($(this).attr('id').replace('expand_',''));
});