[英]window is not loading immediately after clicking a link
當我單擊錨鏈接時,應立即再次加載當前頁面:
<a href="javascript:void(0);" class="BackToFirst"
onclick="popNav('BackToFirst');">Back</a>
function popNav(type) {
if(type == "BackToFirst") {
$(".first").show();
$(".second").hide();
$('.BackToFirst').click(function() {
document.location.href = window.location.href;
});
}
}
我希望當用戶點擊鏈接時,當前頁面會立即加載,但需要一些時間才能加載。
您的Click處理程序僅為鏈接分配一個新的單擊處理程序,請嘗試直接導航:
function popNav(type){
if(type=="BackToFirst")
{
$(".first").show();
$(".second").hide();
document.location.href = window.location.href;
}
}
我認為你有兩個概念混淆了,上面的代碼將DOM事件直接附加到鏈接上,另一種方法是使用JQuery將事件附加到該按鈕,如下所示:
HTML:
<a href="javascript:void(0);" class="BackToFirst">Back</a>
腳本:
$('.BackToFirst').click(function(){
$(".first").show();
$(".second").hide();
document.location.href = window.location.href;
});
如果您仍想檢查類型,那么使用JQuery時數據屬性是一個很好的方法:
<a href="javascript:void(0);" class="BackToFirst" data-linktype="BackToFirst">Back</a>
$('.BackToFirst').click(function(){
if($(this).data('linktype') == 'BackToFirst'){
$(".first").show();
$(".second").hide();
document.location.href = window.location.href;
}
});
我認為你的代碼過於復雜。 單擊元素后綁定onClick。 我認為這樣的事情會更好。
HTML:
<a href="#" class="BackToFirst" onclick="onClickHandler('BackToFirst')">Back</a>
JS:
function onClickHandler(type){
if(type !== 'BackToFirst') {
return;
}
$(".first").show();
$(".second").hide();
location.reload();
}
目前還不清楚你要做什么。
var current = sessionStorage.getItem("which"); // does not run in a snippet
current = current ? current.split("|") : []
if (current.length) {
$("." + current[0]).show();
$("." + current[1]).hide();
}
$(".BackToFirst").on("click", function(e) {
e.preventDefault();
$(".first").show();
$(".second").hide();
sessionStorage.setItem("which", "first|second")
setTimeout(function() {
location.reload(1); // are you absolutely sure this is not an X/Y problem?
}, 500); //let the show/hide sink in
});
<a href="#" class="BackToFirst">Back</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.