javascript 使用 AJAX 请求重新加载整个页面并更改 GET 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9358568/
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
Reload entire page with AJAX request and changed GET parameters
提问by ruhleder
I'm trying to reload an entire page with a jQuery AJAX request (supplying a simple GET parameter to the load), roughly in this fashion.
我正在尝试使用 jQuery AJAX 请求(向加载提供一个简单的 GET 参数)重新加载整个页面,大致以这种方式。
$.ajax({
url: "site.php?param="+new_param,
cache: false,
success: function(content) {
$("html").html(content);
}
});
As you can see, this will cascade the <html>
blocks and somehow the styling gets broken (background is suddenly white and so on).
如您所见,这将级联<html>
块,并且样式会以某种方式被破坏(背景突然变成白色等等)。
Tricks like $(document).html(content);
won't work either.
像这样的伎俩$(document).html(content);
也行不通。
Unfortunately I haven't found any solution to this problem, yet.
不幸的是,我还没有找到解决这个问题的任何方法。
回答by Krule
If you are not reloading head content (loading new javascript or css), and I see no reason why you should do such a thing, you should really try to contain your content loading to content itself, ie. <body>
.
如果您没有重新加载头部内容(加载新的 javascript 或 css),并且我认为您没有理由这样做,那么您应该真正尝试将内容加载到内容本身,即。<body>
.
$("body").html(content);
Edit
编辑
In order to achieve, what I think you are trying to, you could check out https://github.com/defunkt/jquery-pjax
为了实现,我认为您正在尝试,您可以查看https://github.com/defunkt/jquery-pjax
回答by Andrei G
The problem with loading a full HTML page is that you only get the HTML code but the JavaScript is not interpreted, and the paths to the CSS files get messed up. Imagine if you will: you are inside the index.php file and the CSS files are located in the same directory. Now if you load a different HTML file using AJAX, and output that code inside a div, maybe that file points to the "../css/random_dir/style.css" file. Now that path is not good inside your index.php file, and that's why the styling gets messed up. Your new code would expect to find the "background" inside a css file that is not loaded (and will not be loaded).
加载一个完整的 HTML 页面的问题是你只得到了 HTML 代码,但没有解释 JavaScript,并且 CSS 文件的路径变得混乱。想象一下,如果您愿意:您在 index.php 文件中,并且 CSS 文件位于同一目录中。现在,如果您使用 AJAX 加载不同的 HTML 文件,并在 div 中输出该代码,则该文件可能指向“../css/random_dir/style.css”文件。现在,您的 index.php 文件中的路径不好,这就是样式混乱的原因。您的新代码希望在未加载(也不会加载)的 css 文件中找到“背景”。