如何删除javascript函数的onclick属性(已解决)。
答案 0 :(得分:0)
代码中存在两个问题,
_button[k].onclick = return false
时出现语法错误,它应该包含在_button[k].onclick = function() {return false}
之类的函数中,而只是删除onclick
属性。k
时,错误地使用closure variable in a loop将等于_button.length
,因此_button[k]
将返回undefined,这将导致错误,指出无法访问未定义的属性onclick
。尝试
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
属性