jQuery.clone() IE problem

泪湿孤枕 提交于 2019-12-01 17:36:05

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>')
   );
});

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

This seems to work in IE, Firefox & Safari. I'm calling the javascript DOM API cloneNode() method instead of jQuery's clone(). Don't know why it works. You should probably do some more browser testing.

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

I have noticed one big difference in what clone does in IE and others. In IE, it appears to clone everything, including script tags. Therefore if you have code inside the script tag that instantiates code, it will be instantiated again. In the case of this question it is probably encountering an infinite loop since it will continually call the code to clone itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!