简体   繁体   English

为什么“负载”不起作用?

[英]Why the “load” doesn't work?

index.html : index.html:

<div id="divTestArea2"></div>
<script type="text/javascript">

$("#divTestArea2").load("index-2.html #test");

</script>

index-2.html : index-2.html:

<div id="test"><b>Hello World!</b></div>

Why the "load" doesn't work? 为什么“加载”不起作用?

Hypotheses: 假设:

  1. $ is not yet defined; $尚未定义; your script may need to be run after jQuery is included in your page. 在页面中包含jQuery之后,可能需要运行脚本。 You should see a console error if this is the case. 在这种情况下,您应该看到控制台错误。
  2. index-2.html is not a valid relative URL based on the current page's URL. index-2.html基于当前页面的URL是无效的相对URL。 Look at your developer tool's network tab to see if the request for index-2.html is being performed. 查看开发人员工具的“网络”标签,查看是否正在执行对index-2.html的请求。 If you're getting a 404 error, then maybe the URL isn't correct. 如果您收到404错误,则网址可能不正确。

Edit 编辑

Now that you've posted the error, we know the cause is #1; 现在您已经发布了错误,我们知道原因是#1; $ will only be defined if you're including jQuery in your page. 只有在页面中包含jQuery时,才会定义$ If you already have that script tag, move your code to after that. 如果您已经具有该脚本标记,则将代码移到之后。 If you don't, try adding something like this: 如果不这样做,请尝试添加以下内容:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Try to add the # char to select the element by id and put your code inside the ready event. 尝试添加#字符选择由元素id ,并把你的代码里面ready事件。

$(document).ready(function() {

    $("#divTestArea2").load("index-2.html");

});

You could separate the div into another file to get only the necessary part instead trying to read a div inside the file. 您可以将div分离到另一个文件中,以仅获取必要的部分,而不是尝试读取文件中的div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM