为什么赢了';t禁用我的jquery按钮

为什么赢了';t禁用我的jquery按钮,jquery,button,disable-link,Jquery,Button,Disable Link,我试图禁用一个按钮,如果用户当前在该页面上,我知道有更有效的方法,但我正在练习我的if,elses 问题是,我仍然能够单击链接,如果我单击它们并且我在同一页面上,两者都将刷新页面。我的代码: $(“#主页”)。在(“单击”)上,功能(事件){ 如果(window.location.href=='index.html'){ $(this.attr('disabled','disabled'); $(this.attr('disabled',true); event.preventDefault(

我试图禁用一个按钮,如果用户当前在该页面上,我知道有更有效的方法,但我正在练习我的if,elses

问题是,我仍然能够单击链接,如果我单击它们并且我在同一页面上,两者都将刷新页面。我的代码:

$(“#主页”)。在(“单击”)上,功能(事件){
如果(window.location.href=='index.html'){
$(this.attr('disabled','disabled');
$(this.attr('disabled',true);
event.preventDefault();
}否则{
window.location.href='index.html';
};
});	
$(“#aboutUs”)。在(“单击”)上,函数(事件){
如果(window.location.href=='aboutus.html'){
$(this.attr('disabled','disabled');
$(this.attr('disabled',true);
event.preventDefault();
}否则{
window.location.href='aboutus.html';
};

});验证window.location.href的值是多少

$("#homepage").on("click", function(event) {
alert( window.location.href);
});
然后将该值放入如下条件:

if (window.location.href === 'dummyrul/index.html') {
                $(this).attr('disabled', "disabled");
                $(this).attr('disabled', true);
                event.preventDefault();
            }

验证window.location.href的值是多少

$("#homepage").on("click", function(event) {
alert( window.location.href);
});
然后将该值放入如下条件:

if (window.location.href === 'dummyrul/index.html') {
                $(this).attr('disabled', "disabled");
                $(this).attr('disabled', true);
                event.preventDefault();
            }

实际上,
window.location.href
将为您提供完整的URL,如:-

https://www.example.com/index.html

http://www.example.com/index.html
等等

这就是为什么您的
if
条件似乎永远不会变为真

使用
indexOf()
如下所示:-

$("#homepage").on("click", function(event) {
    if (window.location.href.indexOf('index.html') >-1) {
            $(this).prop('disabled', true); // recomended to use `prop`
            event.preventDefault();
    }
    else {
        window.location.href = 'index.html';
    }
}); 
$("#aboutUs").on("click", function(event) {
    if (window.location.href.indexOf('aboutus.html')>-1) {
        $(this).prop('disabled', true);// recomended to use `prop`
        event.preventDefault();
    }
    else {
        window.location.href = 'aboutus.html';
    }
});
参考资料:-


如果只想禁用两个特定URL上的按钮,请在
控制台.log(window.location.href)提供的
If
条件中使用完整路径

实际上
window.location.href
将为您提供完整的URL,如:-

https://www.example.com/index.html

http://www.example.com/index.html
等等

这就是为什么您的
if
条件似乎永远不会变为真

使用
indexOf()
如下所示:-

$("#homepage").on("click", function(event) {
    if (window.location.href.indexOf('index.html') >-1) {
            $(this).prop('disabled', true); // recomended to use `prop`
            event.preventDefault();
    }
    else {
        window.location.href = 'index.html';
    }
}); 
$("#aboutUs").on("click", function(event) {
    if (window.location.href.indexOf('aboutus.html')>-1) {
        $(this).prop('disabled', true);// recomended to use `prop`
        event.preventDefault();
    }
    else {
        window.location.href = 'aboutus.html';
    }
});
参考资料:-



如果只想禁用两个特定URL上的按钮,请在
控制台.log(window.location.href)提供的
If
条件中使用完整路径

记录
window.location.href
是什么。它永远不会是
'index.html'
检查
console.log(window.location.href)
并查看它的输出。我认为这是不一样的,这就是为什么
条件永远不会成功的原因。@AlivetoDie在Google Chrome中,加载时将是
文件://C:\Users\admin\Desktop\MyWebsite\index.html
。这就像
https://www.example.com/index.html
在服务器上,或者在常规IP地址上,
142.153.31.22/index.html
并且永远不只是“index.html”,所以我的理解是正确的,如果不先托管它以获得特定链接,就没有办法这样做吗?@ChristianA使用
indexOf()
如下(在我的回答中). 检查并让我们知道是否工作?记录什么是
window.location.href
。它永远不会是
'index.html'
检查
console.log(window.location.href)
并查看它的输出。我认为这是不一样的,这就是为什么
条件永远不会成功的原因。@AlivetoDie在Google Chrome中,加载时将是
文件://C:\Users\admin\Desktop\MyWebsite\index.html
。这就像
https://www.example.com/index.html
在服务器上,或者在常规IP地址上,
142.153.31.22/index.html
并且永远不只是“index.html”,所以我的理解是正确的,如果不先托管它以获得特定链接,就没有办法这样做吗?@ChristianA使用
indexOf()
如下(在我的回答中). 检查并让我们知道是否有效?@Christian A试试这个one@Christian试一试这个有没有一个简单的方法来解释.indexOf('index.html')>-1是如何工作的?或者一些你可以指点我的方法的文档?我也发现了那个文档,我只是没有用过。indexOf以这种方式带有一个链接和>-1,我会查看文档,希望我能对它有所了解,谢谢如果我理解正确的话,它的本质是运行-1>-1,但为什么不-1==-1?否
indexOf()
给出您正在搜索的字符串的位置,位置从0开始计数。所以它会像0>-1我现在就知道了谢谢,我在控制台上记录了关于tus.html的信息,它给了我-1,因为我在索引页上,所以我很困惑,为什么它会工作。有没有一个简单的方法来解释它是如何工作的。indexOf('index.html')>-1让它工作的?或者一些你可以指点我的方法的文档?我也发现了那个文档,我只是没有用过。indexOf以这种方式带有一个链接和>-1,我会查看文档,希望我能对它有所了解,谢谢如果我理解正确的话,它的本质是运行-1>-1,但为什么不-1==-1?否
indexOf()
给出您正在搜索的字符串的位置,位置从0开始计数。所以它会像0>-1我现在就知道了谢谢,我在控制台上记录了aboutus.html,它给了我-1,因为我在索引页上,所以我很困惑,为什么它能工作