简体   繁体   English

jQuery-将HTML响应加载到整个页面

[英]jQuery - load html response into whole page

In my ajax code, I get a html response. 在我的Ajax代码中,我得到了html响应。 How can I replace the whole page to this html response? 如何将整个页面替换为该html响应? I only find window.location.href , which only can redirct url response. 我只找到window.location.href ,它只能重定向URL响应。

My code: 我的代码:

$('#btn').click(function(){
    $.ajax({
        url: '/someurl',
        data: {'key': value},
        type: 'POST',
        success: function(html_data) {
            # how to load this html_data into the whole page?
        },
        error: ...
    });
 });

jQuery replaceWith should get it done for you ;) jQuery replaceWith应该为您完成;)

$( "html" ).replaceWith( data );

Where data is html received from server... 从服务器接收到html data地方...

Entire code will look something like this... 整个代码看起来像这样...

$.get( "example.com/fileToLoad.html", function( data ) {
  $( "html" ).html( data );
});

Learn more about jQuery here... 在此处了解有关jQuery的更多信息...

http://jquery.com/ http://jquery.com/

window.document is an object containg whole DOM. window.document是包含整个DOM的对象。

Try writing in console window.document and you will see whole structure of a page you're on. 尝试在控制台window.document中编写,您将看到所在页面的整个结构。 If you want only the body element then you can find it by tag and replace what's inside of it. 如果只需要body元素,则可以通过标签找到它,并替换其中的内容。

EDIT 1: 编辑1:

Actually you;re right. 其实你是对的。 I didn't test it. 我没有测试。

But you can do that by: 但是您可以通过以下方式做到这一点:

document.open();
document.write(your_html);
document.close();

Tested in console. 在控制台中测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM