为什么重新添加点击事件没有解雇?

时间:2011-06-26 17:38:14

标签: jquery html

我的HTML呈现如下:

<td colspan="2" onclick="reportTypeClick(this);" class="menuItem oneTest">...</td>

现在我要禁用/启用td以及click事件:

if(...) {
    $('.oneTest').removeClass('menuItem').addClass('menuItemDisabled')
    .attr('onclick', '');
} else {
    $('.oneTest').removeClass('menuItemDisabled').addClass('menuItem')
    .attr('onclick', 'reportTypeClick(this);');
}

if按预期工作,但else没有。也就是说,我没有脚本错误,但事件无法触发。

有什么想法吗?

看起来它与这个有关。

2 个答案:

答案 0 :(得分:3)

您应该删除onclick属性并使用jquery事件处理..

if(...) {
    $('.oneTest').removeClass('menuItem').addClass('menuItemDisabled')
    .unbind('click');
} else {
    $('.oneTest').removeClass('menuItemDisabled').addClass('menuItem').each(function(){
      var _this=this;
      $(this).bind('click', function(){ reportTypeClick(_this) ););
    });
}

或者,您可以更改reportTypeClick功能..

function reportTypeClick(element)
{
  if ( $(element).is('.menuItemDisabled') ) return; // add this line as the first line ..

  // the current body of the function should follow..
}

答案 1 :(得分:1)

您可以使用click功能,而不是通过attr功能添加活动吗?

[edit]并使用jquery unbind函数取消绑定它?

此致

最高

相关问题