0

我正在使用 jquery 加载函数,该函数需要将 div 的内容从页面填充到另一个页面。

第一个.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>

第二个.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 中无法运行。请帮我。

4

6 回答 6

1

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

$(document).ready(function(){
   $("#getit").load("first.html #getdiv");
});
于 2013-05-14T09:24:05.083 回答
1

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

于 2013-10-01T14:34:59.803 回答
0

This is an AJAX request and chrome won't run it if you try to directly access first.html from second.html because of same-origin-policy

Put it on a server (local/non-local) and try to run it like localhost:3000/testProject/second.html , it'll work fine.

于 2013-05-14T09:26:11.013 回答
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 固定到任务栏并右键单击图标,然后右键单击“Google Chrome”并选择“属性”。然后将目标更改为具有新标志,如下所示:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files

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

于 2014-05-13T15:51:32.830 回答
0

使用preventdefault()

$(document).ready(function(){
   event.preventDefault();
   $("#getit").load("first.html #getdiv");
});
于 2014-03-05T12:05:48.997 回答
0

或者你可以像这样启动 Chrome

cd C:\Program Files\Google\Chrome\Application
start chrome.exe C:\second.html --disable-web-security
于 2013-05-14T09:33:00.440 回答