javascript - jQuery.clone() IE问题

标签 javascript jquery internet-explorer clone

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

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script>
$(function(){

  $('button').click(function(){
    var $clone = $('html').clone();
    $('#output').text($clone.html());
  });

});
</script>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <button>run test</button>
  <pre id="output"></pre>
</body>
</html>

IE 是否有任何已知的 bug 可以阻止这种情况,或者我做错了什么?

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

最佳答案

这似乎适用于 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

关于javascript - jQuery.clone() IE问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2874392/

相关文章:

javascript - 如何设置 Nivo Slider 缩略图的样式?

javascript - 空语句在 JavaScript 中作为空操作命令的实际应用

html - 以 Quirks 模式显示的 XSL 网页

html - ie上的RGBA边框

javascript - 为什么我在 IE9 中从 flash 调用 Javascript 会失败?

javascript - Javascript 中的 S3 waitUntilObjectExists

javascript - 使用 if 语句检查元素是否显示 : block jquery

javascript - 这是我调整 iframe 大小并需要一些编辑的 javascript

javascript - Flexslider 未在 Chrome + Safari 中加载第一张幻灯片

javascript - 使用 JavaScript 按 "Delete"键?