我有以下代码:
$('#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工作得很好。我有遗漏什么吗?
发布于 2013-11-18 11:10:52
要使用jQuery删除内联属性,请执行以下操作:
$('#tableA').find('#expand_' + row_id).removeAttr('onclick');但是,对于IE < 9,您应该使用:
.prop('onclick', null);作为在文档中解释。
但是,我确实想知道,为什么您要在ID选择器中使用find。我认为find返回一个jQ对象数组是正确的。没有一个DOM对象。
也许:
$('#expand_' + row_id).prop('onclick', null);是更合适的人选。要用另一个属性替换onclick属性,您不需要2个prop调用,顺便说一句:
$('#expand_' + row_id).prop('onclick', 'expandAndShow('+row_id+')');通过设置另一个处理程序,基本上删除了原始的onclick处理程序。总之,这类事情最好是使用委托来完成:
$('#tableA').on('click', '*[id^=expand_]', function()
{
alert($(this).attr('id').replace('expand_',''));
});此代码处理具有id的#tableA元素的所有子元素上的所有单击事件,以expand_开头。然后,在没有expand_子字符串的情况下,继续警告该id。
发布于 2013-11-18 11:05:32
这应该能行
$('#tableA').find("#expand_" + row_id).unbind('click');
$('#tableA').find("#expand_" + row_id).on('click',expandAndShow(row_id));https://stackoverflow.com/questions/20046170
复制相似问题