jquery load函数无法在firefox中运行的chrome中运行?

时间:2013-05-14 09:19:54

标签: javascript jquery html5 css3

使用jquery load函数,它需要将div的内容从页面填充到另一页面。

first.html

  <!DOCTYPE html>
 <html>
 <head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <script>
 $('#getselect').find('option:selected').text();
 </script>
 </head>
<body>
<div id="getdiv">Not loaded yet.</div>
</body>
 </html>

second.html

 <!DOCTYPE html>
  <html>
  <head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  </head>
  <body>
   <b>Footer navigation:</b>
  <div id="getit"></div>
   <div id="getitselected"></div>
   <script>
  $("#getit").load("first.html #getdiv");
  </script>
  </body>
 </html>

该脚本在firefox中工作正常但不能在chrom中工作。请帮帮我。

6 个答案:

答案 0 :(得分:1)

尝试将代码包装在$(document).ready()

$(document).ready(function(){
   $("#getit").load("first.html #getdiv");
});

答案 1 :(得分:1)

如果您没有在服务器上托管您的网站而只是尝试从浏览器打开.html文件,那么Chrome就无法访问您文件系统中的其他文件。 您必须将项目部署到服务器,例如wamp并进行测试。你会看到chrome工作正常。

答案 2 :(得分:0)

这是一个AJAX请求,如果您因为same-origin-policy

尝试直接从second.html访问first.html,Chrome将无法运行它

将它放在server(本地/非本地)上并尝试像localhost:3000 / testProject / second.html一样运行它,它会正常工作。

答案 3 :(得分:0)

或者您可以像这样启动Chrome

cd C:\Program Files\Google\Chrome\Application
start chrome.exe C:\second.html --disable-web-security

答案 4 :(得分:0)

使用preventdefault()

$(document).ready(function(){
   event.preventDefault();
   $("#getit").load("first.html #getdiv");
});

答案 5 :(得分:0)

请参阅https://stackoverflow.com/a/2990622/1123295http://www.chrome-allow-file-access-from-file.com/

您需要使用--allow-file-access-from-files运行chrome,以使jquery的加载功能在本地运行。

如果在Windows 7+中,将Chrome固定到任务栏并右键单击该图标,然后右键单击&#34; Google Chrome&#34;并选择属性。然后将目标更改为具有新标志,如下所示:

&#34; C:\ Program Files(x86)\ Google \ Chrome \ Application \ chrome.exe&#34; --allow-文件访问从-文件

现在每次启动chrome时,都会使用新策略。