Sin*_*hus 6 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>
Run Code Online (Sandbox Code Playgroud)
有没有知道IE的错误阻止这个,或者我做错了什么?
(我需要克隆它,因为我在输出之前对它进行了一些更改)
这似乎适用于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
Run Code Online (Sandbox Code Playgroud)
如果您只想在页面中显示页面文本,请尝试以下操作:
$('button').click(function(){
$('#output').empty().html(('<html>\n ' + $('html').html() + '\n</html>')
.replace(/&/gm, '&')
.replace(/</gm, '<')
.replace(/>/gm, '>')
.replace(/\r/gm, '')
.replace(/\n/gm, '<br>')
);
});
Run Code Online (Sandbox Code Playgroud)
这对我来说适用于 Chrome、Firefox 和 IE8。
| 归档时间: |
|
| 查看次数: |
8824 次 |
| 最近记录: |