我试图从 textarea
中获取值并将其放入 pre 标签
中,它在 chrome 和 mozilla 上工作正常,但在 IE8 上整个内容在 pre 标签
jsbin 链接:http://jsbin.com/uwunug/4/edit
这就是全部:
<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>
我注意到(通过将 \n
替换为“输入”)\n
字符进入了 pre 但它们不会在那里产生新行
最佳答案
atm 我的解决方案是在末尾添加这一行:
.replace(/\n/g, '<br\>')
在我的htmlEscape中,是这样的:
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\n/g, '<br\>');
}
如果你知道更好的答案,请发表你的答案
关于javascript - pre 中的代码在 IE8 上排成一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7445502/