javascript jquery 加载功能在 chrome 中不起作用,而 chrome 在 Firefox 中起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16539393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery load function not working in chrome which is working in firefox?
提问by Siri Sireesha
im working with jquery load function which require to fill content of a div from page to another page.
我正在使用 jquery 加载功能,该功能需要将 div 的内容从页面填充到另一个页面。
first.html
第一个.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
第二个.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>
The script works fine in firefox but not working in chrom. please help me.
该脚本在 Firefox 中运行良好,但在 chrom 中不起作用。请帮我。
回答by Kevin Bowersox
Try wrapping your code in $(document).ready()
尝试将您的代码包装在 $(document).ready()
$(document).ready(function(){
$("#getit").load("first.html #getdiv");
});
回答by Lem
If you do not host your site on a server and you just try to open an .html file from a browser, in that case chrome cannot access another file in your filesystem. You have to deploy your project to a server e.g wamp and test it. You' ll see chrome works fine.
如果您没有在服务器上托管您的站点,而您只是尝试从浏览器打开 .html 文件,在这种情况下,chrome 无法访问您文件系统中的另一个文件。您必须将项目部署到服务器(例如 wamp)并对其进行测试。你会看到 chrome 工作正常。
回答by Ali.azimi
Use preventdefault()
:
使用preventdefault()
:
$(document).ready(function(){
event.preventDefault();
$("#getit").load("first.html #getdiv");
});
回答by Allen Pestaluky
See https://stackoverflow.com/a/2990622/1123295and http://www.chrome-allow-file-access-from-file.com/
请参阅https://stackoverflow.com/a/2990622/1123295和http://www.chrome-allow-file-access-from-file.com/
You will need to run chrome with --allow-file-access-from-files to get jquery's load function to work locally.
您将需要使用 --allow-file-access-from-files 运行 chrome 以使 jquery 的加载功能在本地工作。
If in Windows 7+, pin Chrome to your taskbar and right click on the icon, then right click on "Google Chrome" and select Properties. Then change the target to have the new flag, something like this:
如果在 Windows 7+ 中,将 Chrome 固定到任务栏并右键单击该图标,然后右键单击“Google Chrome”并选择“属性”。然后将目标更改为具有新标志,如下所示:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files
Now every time chrome launches, it will use the new policy.
现在每次 chrome 启动时,它都会使用新策略。
回答by Harsha Venkatram
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
这是一个 AJAX 请求,如果您尝试从 second.html 直接访问 first.html,chrome 将不会运行它,因为 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.
将它放在server
(本地/非本地)上并尝试像 localhost:3000/testProject/second.html 一样运行它,它会正常工作。
回答by gaepi
Or you can start Chrome like this
或者你可以这样启动 Chrome
cd C:\Program Files\Google\Chrome\Application
start chrome.exe C:\second.html --disable-web-security