Joe*_*Joe 1 javascript ajax jquery web
在我的Ajax代码中,我得到了html响应。如何将整个页面替换为该html响应?我只找到window.location.href
,它只能重新分配网址响应。
我的代码:
$('#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: ...
});
});
Run Code Online (Sandbox Code Playgroud)
window.document 是一个包含整个 DOM 的对象。
尝试在控制台 window.document 中编写,您将看到您所在页面的完整结构。如果您只想要 body 元素,那么您可以通过标签找到它并替换其中的内容。
编辑1:
其实你是对的。我没有测试过。
但你可以通过以下方式做到这一点:
document.open();
document.write(your_html);
document.close();
Run Code Online (Sandbox Code Playgroud)
在控制台中测试。
jQuery replaceWith
应该为您完成它;)
$( "html" ).replaceWith( data );
Run Code Online (Sandbox Code Playgroud)
凡data
被html
从服务器接收...
整个代码看起来像这样...
$.get( "example.com/fileToLoad.html", function( data ) {
$( "html" ).html( data );
});
Run Code Online (Sandbox Code Playgroud)
在此处了解有关jQuery的更多信息...
归档时间: |
|
查看次数: |
5561 次 |
最近记录: |