如何用jQuery选择和替换整个页面
我的页面设计迫使我用我通过ajax加载的html刷新整个页面。
$('html').replaceWith(data);
给我错误。 有任何想法吗?
我有同样的问题,但这没有帮助。 如果你还需要替换标签(所以,整个页面),你也可以这样做
document.write(newPage);
使用身体:
$('body').replaceWith(data);
我遇到了一些问题
$("body").replaceWith(newPage)
给我一些奇怪的CSS问题,但这工作得很好:
$("body").html(newPage);
使用’body’选择器执行jQuery.replaceWith和jQuery.html时的奇怪行为。 你打电话后松开身体标签:
$('body').replaceWith('New body');
任何其他选择器都不会发生这种情况:
$('title').replaceWith('New title ');
此外,jQuery.html不会将body标记加倍(与其他标记一样),并在调用时像replaceWith一样操作:
$('body').html('New body');
我希望这不是jQuery的灰色区域。 或者如果是,他们不考虑修复它。 我有应用程序,我使用$(’body’)。html当$(’body’)。replaceWith应该使用。
这可能是您需要的解决方案, 它取代了整个文档,包括其头部 。 加载新的css,js和其他资源时不会有任何问题。 我假设您通过调用REST API来接收新的html内容:
$.ajax({ type: 'POST', url: form.attr('action'), data: form.serialize(), // serializes form elements success: function(response) { // re-writes the entire document var newDoc = document.open("text/html", "replace"); newDoc.write(response); newDoc.close(); } });