18910140161

JavaScript中带有加载器div的哈希URL

顺晟科技

2021-07-04 22:13:22

153

我已经通过JavaScript创建了哈希URL加载函数。我面临一个问题,即单击href标记时无法显示加载div。

我的代码工作正常,当我重新加载页面时,它只将加载div显示为<div class="loader"><div>,但它在单击链接上不工作。

请帮助我,我如何在单击哈希链接时实现该函数。

function loadcontent(hash) { if (hash == "") { hash = "./default-page.html"; } $("html body").animate({ scrollTop: 0 }, "600", "swing"); $("div.main-content-inner").load("pagefolder/" + hash); } $(window).on("hashchange", function() { loadcontent(location.hash.slice(1)); }); var url = window.location.href; var hash = url.substring(url.indexOf("#") + 1); if (hash === url) { hash = "./default-page.html"; } $("body").append('<div class="loader"><div>'); $("body> div").load("pages/mypage/" + hash, function(responseTxt, statusTxt, jqXHR) { if (statusTxt == "success") { $("div.loader").remove(); } else { if (statusTxt == "error") { $("div.loader").remove(); alert("Error : " + jqXHR.status + " " + jqXHR.statusText); return false; } } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#home">home</a> <a href="#about">about</a> <a href="#contactus">contact us</a>


顺晟科技:

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航