首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

textarea 光标安插

2012-10-31 
textarea 光标插入textarea idtarget_areaonclicksetCaret(this) onkeyupsetCaret(this)/te

textarea 光标插入

<textarea id='target_area'onclick="setCaret(this);" onkeyup="setCaret(this);"></textarea>


//设置textarea光标位置function setCaret(textObj){if(textObj.createTextRange){     textObj.caretPos=document.selection.createRange().duplicate();  }}//光标位置插入内容function insertAtCaret(textFeildValue){var textObj = document.getElementById('target_area');if(firefox){var start = textObj.selectionStart;var end = textObj.selectionEnd;textObj.value = textObj.value.substr(0,start) + textFeildValue + textObj.value.substr(end);}    else{if(textObj.createTextRange && textObj.caretPos){     var caretPos=textObj.caretPos;    caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==''?textFeildValue+'':textFeildValue;}else {  textObj.value+=textFeildValue;}}}

热点排行