jQuery.clone()IE问题

我有一些使用jQuery.clone()来获取页面的html,然后将其添加到pre标签。 它在Firefox和Chrome中正常工作,但在IE中没有任何反应:

     JS Bin   $(function(){ $('button').click(function(){ var $clone = $('html').clone(); $('#output').text($clone.html()); }); });   article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; }     

是否有任何知道IE的错误阻止了这个,或者我做错了什么?

(我需要克隆它,因为我在输出之前对它进行了一些更改)

如果您只想在页面中显示页面文本,请尝试以下操作:

 $('button').click(function(){ $('#output').empty().html(('\n ' + $('html').html() + '\n') .replace(/&/gm, '&') .replace(//gm, '>') .replace(/\r/gm, '') .replace(/\n/gm, '
') ); });

这适用于Chrome,Firefox和IE8。

这似乎适用于IE,Firefox和Safari。 我正在调用javascript DOM API cloneNode()方法而不是jQuery的clone() 。 不知道为什么会这样。 你可能应该做更多的浏览器测试。

 var $scripts = $('script'); // Cache all scripts in the document var html = $('html').get(0).cloneNode(true); // Clone HTML using DOM API var $html = $(html); // Make jQuery object from cloned HTML $('script', $html).each(function(i) { // Loop through scripts in $html this.text = $scripts.get(i).innerHTML; // replacing content with that }); // from the cached $scripts $('#output').empty().text($html.html()); // Append to #output 

我注意到克隆在IE和其他方面的作用有一个很大的不同。 在IE中,它似乎克隆了所有内容,包括脚本标记。 因此,如果脚本标记中的代码实例化代码,则会再次实例化它。 在这个问题的情况下,它可能遇到无限循环,因为它将不断调用代码来克隆自己。