jQuery.clone()IE问题 [英] jQuery.clone() IE problem

查看:160
本文介绍了jQuery.clone()IE问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用jQuery.clone()来获取页面的html,然后将其添加到pre标签中.它可以在Firefox和Chrome中正常运行,但在IE中什么也没发生:

I'm have some that uses jQuery.clone() to get the html of a page and then add it to a pre tag. It works correctly in Firefox and Chrome, but nothing happens in 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是否存在阻止此错误的已知错误,或者我做错了什么?

Is there any know bug with IE that prevents this, or am I doing something wrong?

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

推荐答案

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

If you want to just show the page text in the page, try this:

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

这对我在Chrome,Firefox和IE8中有效.

That works for me in Chrome, Firefox, and IE8.

这篇关于jQuery.clone()IE问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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