ユーザーが現在そのページにいる場合、ボタンを無効にしようとしていますが、より効率的な方法がありますが、if、elsesを練習しています。jqueryボタンが無効にならない理由
問題は、リンクをクリックすることができて、リンクをクリックするとページが更新され、同じページにあることです。私のコード:
$("#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';
};
}); \t
$("#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例えば、Google Chromeでは、読み込まれると 'file:// C:\ Users \ admin \ Desktop \ MyWebsite \ index.html'になります。サーバ上、または通常のIPアドレス '142.153.31.22/index.html'上では' https:// www.example.com/index.html'のようになり、決して** ** "index.htmlにはなりません" –