【发布时间】:2017-03-16 04:45:28
【问题描述】:
如果用户当前在该页面上,我正在尝试禁用按钮,我知道有更有效的方法,但我正在练习我的 if,否则。
问题是我仍然可以单击链接,如果我单击它们并且我在同一页面上,两者都会刷新页面。我的代码:
$("#homepage").on("click", function(event) {
if (window.location.href === 'index.html') {
$(this).attr('disabled', "disabled");
$(this).attr('disabled', true);
event.preventDefault();
}else {
window.location.href = 'index.html';
};
});
$("#aboutUs").on("click", function(event) {
if (window.location.href === 'aboutus.html') {
$(this).attr('disabled', "disabled");
$(this).attr('disabled', true);
event.preventDefault();
}else {
window.location.href = 'aboutus.html';
};
});
【问题讨论】:
-
记录
window.location.href是什么。永远不会是'index.html' -
检查
console.log(window.location.href)看看它的输出。我认为这是不同的东西,这就是为什么if条件永远不会成功。 -
@AlivetoDie 在谷歌浏览器中,例如,加载时它将是
file://C:\Users\admin\Desktop\MyWebsite\index.html。它就像在服务器上的https://www.example.com/index.html,或者在常规IP 地址142.153.31.22/index.html上,并且永远不会只是“index.html” -
所以我是否正确地阅读了这一点,如果不先托管它以获取特定链接,就没有办法做到这一点?
-
@ChristianA 使用
indexOf(),如下所示(在我的回答中)。检查并告诉我们是否有效?
标签: jquery button disable-link