pre内部的代码在IE8上只有一行 [英] code inside pre goes in one line on IE8
问题描述
我试图从 textarea
取值,并将它放在 pre标签
中,在chrome和mozilla上工作正常,但在IE8上,整个内容保留在 pre标记中的一行上
I'm trying to take the value from a textarea
and put it inside a pre tag
, and it works ok on chrome and mozilla but on IE8 the entire content stays on one line in the pre tag
jsbin link : http://jsbin.com/uwunug/4/edit
jsbin link: http://jsbin.com/uwunug/4/edit
这是整件事:
this is the whole thing:
<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<script type='text/javascript'>
$(function(){
$('#b1').click(function(){
x = $('textarea').val();
$('#tt').html(htmlEscape(x));
});
});
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
</script>
<textarea cols='50' rows='20'>
</textarea>
<button id='b1'>make code</button>
<pre class="prettyprint" id='tt'>
</pre>
</body>
</html>
我注意到(通过将 \\\
替换为'enter')
\\\
字符进入pre,但不会在那里产生新行
I noticed (by replacing \n
to 'enter') the \n
chars go into the pre but they don't produce new line in there
推荐答案
IE将HTML标准化应用于分配给innerHTML属性的数据。这会导致在应该保留格式的元素中显示不正确的空白,例如
< pre>
和< textarea>
。
\\\
解决方案可能是您最好的选择(唯一的选择是使用innerText,但我更喜欢您的解决方案)。
The \n
solution is probably going to be your best shot (the only other option is to use innerText it seems, but I prefer your solution).
这篇关于pre内部的代码在IE8上只有一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!