使用JavaScript动态的绑定、解绑 a 标签的onclick事件,防止重复点击

时间:2023-03-09 04:58:20
使用JavaScript动态的绑定、解绑 a 标签的onclick事件,防止重复点击
页面上的 a 标签如下:
<a class="more" style="cursor: pointer;" id="commentMore"
onclick="javascript:selectMoreComment(this.id,2018);">查看更多</a>
页面操作元素的js代码:
function selectMoreComment(id, selectVal){
document.getElementById(id).onclick = null; // 解绑onclick事件 alert("点击事件已经取消,当前业务处理完后再把点击事件加上,可以防止数据加载期间用户的重复点击操作!") // 绑定onclick点击事件
document.getElementById(id).onclick = function () {
selectMoreComment(id, selectVal)
}
}