jQuery 替换所有 HTML [英] jQuery replace all HTML
问题描述
我正在尝试用另一个页面替换所有 HTML(包括 HTML 标签).我想要做的是让网站充当应用程序,即使在导航另一个页面时也是如此.
I'm trying to replace all the HTML (including the HTML tags) with another page. What I'm trying to do is having a website acting as an app even when navigating the another page.
代码如下:
(function($) {
Drupal.behaviors.loadingPage = {
attach: function(context,settings) {
$('a').click(function(event) {
event.preventDefault();
// Create the loading icon
// ...
$.ajax({
url: $(this).attr('href'),
success: function(data) {
$('html').replaceWith(data);
}
});
});
}
};
})(jQuery);
我尝试了几件事.删除 HTML 标签后,replaceWith() 在 jquery.js 中导致 jQuery 错误,我猜是因为它无法再找到父级来添加替换.
I've tried several things. replaceWith() causes a jQuery error in jquery.js after deleting the HTML tag, I guess it is because it can't find the parent anymore to add the replacement.
我得到的最好结果是使用 document.write(data)
.问题是,加载页面上的javascript代码没有执行.
The best result I got was with document.write(data)
. The problem is, the javascript code on the loaded page is not executed.
有人有更好的主意吗?
推荐答案
更好的主意?是的,只需通过设置 window.location
而不是使用 AJAX 来正常加载新页面.或提交表单.
A better idea? Yeah, just load the new page normally by setting window.location
instead of using AJAX. Or submit a form.
否则,如果您想加载一些内容来替换当前页面的所有可见内容,但要保持当前页面的脚本继续运行,请将所有可见内容放在一个大小以填充浏览器窗口的框架中 - 这再次,您不会使用 AJAX 填充.
Otherwise, if you want to load something to replace all of the visible content of the current page but keep the current page's script going, put all the visible content in a frame sized to fill the browser window - which, again, you wouldn't populate using AJAX.
(我喜欢 AJAX,但我不明白你为什么要用它来替换整个页面.)
(I like AJAX, but I don't see why you'd use it to replace a whole page.)
这篇关于jQuery 替换所有 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!