我的js为什么会触发两次

$(function() {
$(".show").off("click").on("click",function(e){
e.preventDefault();
var new_Id=this.dataset.index;
var new_GrpId=this.dataset.indexNew_grpid;
if(new_Id!=null&&new_GrpId!=null){
location.href="getNewContentById.action?new_Id="+new_Id+"&new_GrpId="+new_GrpId;
}
e.stopPropagation();    //阻止冒泡  
});

})

我们在监听事件里面加上一句e.preventDefault();就可以阻止a标签的事件发生了,这样就不会再触发两次了,

或者是更简单的方法是把a标签里面的herf去掉,就不会再触发a标签href的事件了。

猜你喜欢

转载自www.cnblogs.com/jkr666666/p/10279667.html