jQuery.replaceWith不采取<脚本>标签脚本、标签、replaceWith、jQuery

2023-09-10 13:47:31 作者:从清晨到日暮

我有一个Ajax响应,其中包含一些HTML,包括<脚本> 元素

I have an AJAX response that contains some HTML, including <script> elements.

这个HTML应该替​​换现有的元素,但是当我使用:

This HTML should replace an existing element, but when I use:

$(element).replaceWith($(response))

我失去了脚本标记从它...

I lose the script tags from it...

我如何可以将它究竟是如何发送的HTML?

How can I insert the HTML exactly how it is sent?

推荐答案

这code是Facebook的示例应用程序。插入&LT; D​​IV ID =FB-根&GT;&LT; / DIV&GT; 在文档中。在code要求jQuery来加载脚本。在你的情况下,更换 //连接..... / all.js的&LT;脚本&GT; 元素。

This code is from facebook's sample app. Insert <div id="fb-root"></div> in your document. The code requires jQuery to load up the script. In your case, replace //connect...../all.js' with the <script> elements.

(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());