把textarea中字符串里含有的回车换行替换成<br>的javascript代码


1楼
textarea.value.replace(/\n/g, "<br>");
2楼
s=textarea.value.replace(/\r\n/g, "<br>");

3楼
按下的时候马上更换
V ID="a" contentEditable="true" style='border:1 solid black'>
其实以前早有很多人讨论过,代码比较多方法太可怕,自己用不到就没时间去研究,现在帮你试了一下,其实解决起来很简单了:)
<p>关于微软HTML编辑控件单击回车会插入<p>而不是<br>的解决方案</DIV> <p>
<div style='border:1 solid black' contentEditable="true">未经过脚本处理的编辑控件</div>
<script>
function a.onkeypress(){
if(event.keyCode==13){
var txtobj=document.selection.createRange()
txtobj.text==""?txtobj.text="\n":(document.selection.clear())&(txtobj.text="\n") //三目复合表达式,解决有被选文字时回车的光标定位问题
document.selection.createRange().select()
return false
}}
</script>
4楼
按下的时候马上更换
V ID="a" contentEditable="true" style='border:1 solid black'>
其实以前早有很多人讨论过,代码比较多方法太可怕,自己用不到就没时间去研究,现在帮你试了一下,其实解决起来很简单了:)
<p>关于微软HTML编辑控件单击回车会插入<p>而不是<br>的解决方案</DIV> <p>
<div style='border:1 solid black' contentEditable="true">未经过脚本处理的编辑控件</div>
<script>
function a.onkeypress(){
if(event.keyCode==13){
var txtobj=document.selection.createRange()
txtobj.text==""?txtobj.text="\n":(document.selection.clear())&(txtobj.text="\n") //三目复合表达式,解决有被选文字时回车的光标定位问题
document.selection.createRange().select()
return false
}}
</script>
5楼
按下的时候马上更换
V ID="a" contentEditable="true" style='border:1 solid black'>
其实以前早有很多人讨论过,代码比较多方法太可怕,自己用不到就没时间去研究,现在帮你试了一下,其实解决起来很简单了:)
<p>关于微软HTML编辑控件单击回车会插入<p>而不是<br>的解决方案</DIV> <p>
<div style='border:1 solid black' contentEditable="true">未经过脚本处理的编辑控件</div>
<script>
function a.onkeypress(){
if(event.keyCode==13){
var txtobj=document.selection.createRange()
txtobj.text==""?txtobj.text="\n":(document.selection.clear())&(txtobj.text="\n") //三目复合表达式,解决有被选文字时回车的光标定位问题
document.selection.createRange().select()
return false
}}
</script>

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3