我在解析文本区域中的回车时遇到了一个小问题。
JQuery 代码
$.fn.escapeHtml = function() {
this.each(function() {
$(this).html(
$(this).html()
.replace(/"/g,""")
.replace(/&/g,'&')
.replace(/</g,'<')
.replace(/>/g,'>')
.replace(/'/g,''')
// For converting carrage return (enter key) to br
.replace(/(\r\n|\r|\n)/g, '< br >')
.replace(/183/g,'·')
);
});
return $(this);
}
})(jQuery);
在 Firefox 中它工作并给出输出为
<big>Heading</big><br>Test Line 1<br>Test Line 2<br>Test Line 3
在 Internet Explorer 中它根本不起作用。我如何在 IE 中修复此问题。我的预期输出是这样的
<big>Heading</big><br>Test Line 1<br>Test Line 2<br>Test Line 3
提前致谢...
最佳答案
尝试替换:
.replace(/(\r\n|\r|\n)/g, '< br >')
与:
.replace(/(\n\n|\r|\n)/g, '< br >')
另请注意,在 textarea 中,它们都归结为 \n
,因此您需要替换为其中的两个,例如 \n\n
。
关于javascript - 将 Carrage 返回转换为 br 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3340870/