将一个HTML标记替换为另一个 [英] Replace one HTML tag with another

查看:134
本文介绍了将一个HTML标记替换为另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当发布ajax帖子时,都会返回一个包含html 4 doctype的完整页面,但我想在当前页面上显示该页面。返回页面的head标签中还有一些脚本包含在document.ready中,这意味着我的选项仅限于 $。parseHTML 即需要执行它们每当新文档完全加载时。下面是我正在使用的当前代码



我尝试过:



Whenever an ajax post is made, a full page with html 4 doctype is returned but I want to display that page on the current one. There are also some scripts in the head tag of that returned page that are wrapped in document.ready, which means my options are limited to $.parseHTML i.e. they need to be executed whenever the new document is fully loaded. Below is the current code I'm using

What I have tried:

$.post('tet.php', {key: JSON.stringify(payload)}, function(res) {

// res truly returns a full page
 var newPage = $.parseHTML($.trim(res), null, true).find(function (e) {
 try {
  return e.matches('#container'); // this is the one tag I'm interested in
 }
 catch (e) {
  return false;
 }
 });

 $('body').replaceWith(newPage); // errors originate from this line
});





现在上面的代码不断抛出所有类型的错误,围绕缺少 documentFragment 或缺少 clientWidth 等。如何在仍然能够在其head标签中执行脚本的情况下选择该元素并加载到DOM中?如果那是不可能的,那么如何删除当前文档并安装返回的文档?



Now the code above keeps throwing all sorts of errors revolving around lack of documentFragment or missing clientWidth etc. How do I go about picking out that element and loading into the DOM while still being able to execute the script in its head tag? If that isn't possible, then how can I remove the current document and install the returned one?

推荐答案

.parseHTML 即他们需要在新文档完全加载时执行。下面是我正在使用的当前代码



我尝试过:



.parseHTML i.e. they need to be executed whenever the new document is fully loaded. Below is the current code I'm using

What I have tried:


.post(' tet.php',{key: JSON .stringify(payload)}, function ( res){

// res真正返回整页
var newPage =
.post('tet.php', {key: JSON.stringify(payload)}, function(res) { // res truly returns a full page var newPage =


.parseHTML(
.parseHTML(


这篇关于将一个HTML标记替换为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆