jQuery.clone () проблема IE

Я, имеют некоторых, который использует jQuery.clone (), чтобы получить HTML страницы и затем добавить его к пред тег. Это работает правильно в 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, который предотвращает это, или я делаю что-то не так?

(Я должен клонировать его, потому что я делаю некоторые изменения в нем прежде, чем произвести его),

6
задан slugster 1 December 2010 в 06:48
поделиться

2 ответа

Если вы хотите просто показать текст страницы на странице, попробуйте следующее:

$('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.

2
ответ дан 10 December 2019 в 00:35
поделиться

Похоже, это работает в 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
7
ответ дан 10 December 2019 в 00:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: