我有 JQuery 代码:
$("#step1Skip").on("click", function(event){
$('#chooseTheme, #addImages, #addVideo, #finalStep').hide();
$('#addLogo').show();
});
如果我使用按钮,例如
<button type="button" id="step1Skip">Skip this step >></button>
这很好用。但是,如果我尝试使用简单的链接进行相同的操作:
<a href="#" id="step1Skip">Skip this step</a>
它什么也没做,根本不起作用。这是为什么?
我不会同时尝试它们,所以这不是 ID 问题。
最佳答案
您的浏览器正在尝试访问该链接。尝试使用.preventDefault()
$("#step1Skip").on("click", function(event){
event.preventDefault();
$('#chooseTheme, #addImages, #addVideo, #finalStep').hide();
$('#addLogo').show();
});
这将阻止发生链接的默认操作。
关于jquery - 按钮正常,但 href 链接无法触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17175472/