RT 将textarea 中的文本选中后 按下一个button 实现 替换效果~~~~~
答案:1 悬赏:50 手机版
解决时间 2021-02-09 13:27
- 提问者网友:动次大次蹦擦擦
- 2021-02-08 21:55
js 实现 替换选中的文本
最佳答案
- 五星知识达人网友:底特律间谍
- 2021-02-08 22:47
<script><br> function setCaret(){<br> if(this.createTextRange){<br> this.caretPos=document.selection.createRange().duplicate();<br> }<br> };<br><br>//保存当前光标位置,并且插入当前位置<br> function insertAtCaret(textObj,textFeildValue){<br> if(document.all){//ie<br> if(textObj.createTextRange&&textObj.caretPos){<br> var caretPos=textObj.caretPos;<br> //alert(caretPos.text)<br> caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==''?textFeildValue+'':textFeildValue;<br> caretPos.select();<br> }else {<br> textObj.value=textFeildValue;<br> }<br> }else {//ff<br> if(textObj.setSelectionRange){<br> var rangeStart=textObj.selectionStart;<br> var rangeEnd=textObj.selectionEnd;<br> var oldScrollTop = textObj.scrollTop;<br> var oldScrollHeight = textObj.scrollHeight;<br> var oldLen = textObj.value.length;<br> var tempStr1=textObj.value.substring(0,rangeStart);<br> var tempStr2=textObj.value.substring(rangeEnd);<br> textObj.value=tempStr1+textFeildValue+tempStr2;<br> textObj.selectionStart = textObj.selectionEnd = rangeStart + textFeildValue.length;<br> if (textObj.value.length == oldLen) {<br> textObj.scrollTop = textObj.scrollHeight;<br> } else if (textObj.scrollHeight > oldScrollHeight) {<br> textObj.scrollTop = oldScrollTop + textObj.scrollHeight - oldScrollHeight;<br> } else {<br> textObj.scrollTop = oldScrollTop;<br> }<br> }else {<br> alert("xxxxx");<br> }<br> }<br> }<br></script><br><br><textarea id="oTextArea" onselect="setCaret()" onclick="setCaret()" onkeyup="setCaret()">bbbbb</textarea><input type="button" onclick='insertAtCaret(document.getElementById("oTextArea"),"iii")' value="replace"/>
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯