单击按钮时忽略运行代码的最佳方法是什么?我正在尝试以下但我现在没有反应我想要它完成。
if (!document.getElementById('btn_Cancel').getAttribute('onclick')) {
// code not to be ran when button is clicked
By default code gets ran when a textbox goes onblur so do not want that code ran when button gets clicked
}
答案 0 :(得分:0)
您将要对该元素进行引用,因此每次单击时您都不会查找它,并且您需要一个变量来跟踪它是否被单击:< / p>
var cancelButton = document.getElementById('btn_Cancel'),
clicked = false;
cancelButton.addEventListener('click', function() { clicked = !clicked; }, false);
// assuming this is in a loop or something:
if(!clicked) {
// running code
}
else {
// clicked, do nothing
}