删除onclick函数的javascript属性?

时间:2013-12-10 02:46:04

标签: html

如何删除javascript函数的onclick属性(已解决)。

2 个答案:

答案 0 :(得分:0)

代码中存在两个问题,

  1. 分配_button[k].onclick = return false时出现语法错误,它应该包含在_button[k].onclick = function() {return false}之类的函数中,而只是删除onclick属性。
  2. 当调用onclick处理程序k时,错误地使用closure variable in a loop将等于_button.length,因此_button[k]将返回undefined,这将导致错误,指出无法访问未定义的属性onclick
  3. 尝试

    for (var k = 0; k < _button.length; k++) {
        _button[k].onclick = function (e) {
            _counter++;
    
            if (_counter > _sArray.length - 1) {
                //here this refers to the button
                delete this.onclick;
                //the button and the element with said class are the same so you can use this.innerHTML to set the text
                this.innerHTML = 'Done';
            } else {
                update();
            };
    
            e.preventDefault();
            return false;
        };
    };
    

答案 1 :(得分:0)

使用 JQuery ,以下内容会从按钮中移除onclick属性:$("#buttonId").removeAttr("onclick"),其中buttonId是按钮的id属性