Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么jQuery';s load()需要额外的代码吗?这个额外的代码应该是什么?_Javascript_Jquery_Html_Modularity - Fatal编程技术网

Javascript 为什么jQuery';s load()需要额外的代码吗?这个额外的代码应该是什么?

Javascript 为什么jQuery';s load()需要额外的代码吗?这个额外的代码应该是什么?,javascript,jquery,html,modularity,Javascript,Jquery,Html,Modularity,在我的index.html中,我有一堆空的节标记。每个节标记从单独的文件接收各自的HTML代码。我的设置如下所示: index.html <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Using jQuery's load()</title> </head> <body> <se

在我的index.html中,我有一堆空的节标记。每个节标记从单独的文件接收各自的HTML代码。我的设置如下所示:

index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Using jQuery's load()</title>
</head>
<body>
    <section id="section1"></section>
    <section id="section2"></section>
    <section id="section3"></section>
    <script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
    <script src="assets/js/section-loader.js"></script>
</body>
</html>
<h1>Section 1</h1>
<h2>Section 2</h2>
<h3>Section 3</h3>
当我第一次加载所有内容时,它会将每个部分的HTML加载到index.HTML中各自的部分标记中。这正是我希望发生的事情

但是,当我刷新页面或打开页面的新实例时,这些加载调用都不会发生

我向section-loader.js添加了一个警报,以查看该页面是否第二次被调用,并收到了意外的结果。该警报使所有内容都以我想要的方式第二次运行(除了不需要的警报)

我希望我已经解释得足够清楚了。这对我来说是很奇怪的行为

我有两个问题:

  • 为什么会这样
  • 最好的解决方法是什么

您必须等待onload事件,对于jQuery:

$(function() {
    $('#section1').load('section1.html');
    $('#section2').load('section2.html');
    $('#section3').load('section3.html');
});

重新加载(devtools)后,“网络”选项卡中会发生什么情况?请尝试在
部分loader.js中运行
$(document.ready()
@OddDev中的代码200s@SalmanA我也是这么想的,但似乎有一些奇怪的时间安排。因为这只会发生第二次,我怀疑它也涉及到缓存。@Evorlor能否请您在Chrome的devtools中禁用缓存(F12->cog->勾选“禁用缓存(devtools打开时)”)在DevTools仍然打开的情况下重新加载吗?这不是
onload
事件,而是jQuery
ready
伪事件的简写。只会导致没有一个节被加载。@evorl它可以工作!
$(function() {
    $('#section1').load('section1.html');
    $('#section2').load('section2.html');
    $('#section3').load('section3.html');
});