Here is an example of the problem 然而,如果你去directly to the navigation page ,它会立即加载。
我认为这是因为我在单独的 html 文件中创建基础顶部栏并使用此脚本调用。
<script>
$(function(){
$("#includeHeader").load("Navigation.html");
});
</script>
然后,紧跟在开始的 body 标签之后
<div class="fixed shadow" id="includeHeader"></div>
有没有更好的调用外部文件的方法,还是我应该将导航代码分别复制到每个 HTML 页面中? (不过,当我想进行更改时,这似乎有点愚蠢)。
最佳答案
您的代码会在整个页面加载后进行评估。 ( http://www.w3schools.com/jquery/jquery_syntax.asp )
如果您希望额外的 html 与页面的其余部分同时呈现,则必须将其复制粘贴到页面中或通过服务器端语言(例如 php)加载其内容
$(function(){ // document-ready event
// code is executed after the page loads
});
在 PHP 中,您可以这样做:
<?php
$header = file_get_contents('/path/to/header.html');
echo $header;
?>
关于javascript - 导航栏加载延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411579/