我正在嘗試創建一個全局頁面,它只是更改內容而不是完全加載新頁面。 當我嘗試在home.php頁面上調用loadPage函數時,它只是告訴我loadPage函數沒有被定義。 的問題是,我認爲,該函數具有當新的內容被加載到被重新建立......jQuery全局加載頁面問題
全球網頁代碼(HTML):
<!-- Please note: this is the part that loads the page, and displays the loading image -->
<script type="text/javascript" src="jQuery/jQuery.js"></script>
<script type="text/javascript" src="js/loadPage.js"></script>
<link type="text/css" rel="stylesheet" href="css/style-loading.css">
<img class="loading-img" src="images/loading.gif">
<!-- End of note -->
<!doctype html>
<html>
</html>
頁面加載代碼(JS):
//Copyright © 2016 Dynavio Cooperative
var currentPage = null;
$(document).ready(function()
{
loadPage();
});
function loadPage(pageToLoad)
{
$(".loading-img").removeAttr("style");
if(typeof pageToLoad == "undefined")
{
if(currentPage == null)
{
$("html").load("pages/home.php", function()
{
$(".loading-img").css("display", "none");
});
}
}
else
{
$("html").load(pageToLoad, function()
{
$(".loading-img").css("display", "none");
});
}
}
home.php代碼(HTML):
<!-- Copyright © 2016 Dynavio Cooperative -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Dynavio">
<meta name="keywords" content="Dynavio,Cooperative,Administration">
<meta name="author" content="Dynavio Cooperative">
<title>Dynavio - Homepage</title>
<!-- Css styles -->
<link type="text/css" rel="stylesheet" href="css/plugins/font-awesome-4.6.1/css/font-awesome.min.css">
<link type="text/css" rel="stylesheet" href="css/style-main-global.css">
<link type="text/css" rel="stylesheet" href="css/style-fonts.css">
<link type="text/css" rel="stylesheet" href="css/style-navbar.css">
<link type="text/css" rel="stylesheet" href="css/style-slider.css">
<!-- End of Css styles -->
<script type="text/javascript" src="jQuery/plugins/Slides-SlidesJS-3/source/jquery.slides.min.js"></script>
<script type="text/javascript" src="js/sliderInit.js"></script>
<!-- Javascript -->
</head>
<body>
<div class="navbar">
<img src="images/logo.png" class="nav-logo">
<ul class="nav-items">
<li class="nav-item active">Homepage</li>
<li class="nav-item">Products</li>
<li class="nav-item">Company</li>
<li class="nav-item">Research</li>
<li class="nav-item">Contact</li>
<li class="nav-item" onClick="loadpage(pages/login.php);">Login</li>
</ul>
</div>
<div class="intro-slider slider">
<img src="images/example1.png">
<img src="images/example2.png">
</div>
<div class="products">
</div>
</body>
<!-- Copyright © 2016 Dynavio Cooperative -->
該函數不需要重新創建。在你的home.php文件中,你是否在加載JS文件之前或之後調用函數?如果你在加載文件之前調用函數,那麼它將是未定義的。由於這看起來像你的HTML的結束,我想你不得不加載你的JS上面的函數調用。 – JeremyS
或者,也許問題是,我實際上重新鏈接home.php中的jquery文件 – Jojo01
你在哪裏調用實際調用Home.php中的loadPage()函數?我沒有看到上面的代碼。你也加載jQuery文件兩次?我也沒有看到。 – JeremyS