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

怎么选中textarea中指定文字

2012-09-16 
如何选中textarea中指定文字选中 textarea中指定文字textarea cols40 rows10 idtargetdivphp

如何选中textarea中指定文字
选中 textarea中指定文字

<textarea cols="40" rows="10" id="target">
<div>php
<div>qqq</div>
</textarea>



怎么让 

<div>php</div>
是选中状态

注:textarea 中可以看到 div标记

[解决办法]

HTML code
<script>function foo(){   var textbox=  document.getElementById('target');   selectText(textbox,0,14);}function selectText(textbox,startIndex,stopIndex){     if(textbox.setSelectionRange){           textbox.setSelectionRange(startIndex,stopIndex);      }else if(textbox.createTextRange){         var range=textbox.createTextRange();         range.collapse(true);         range.moveStart('character',startIndex);         range.moveEnd('character',stopIndex-startIndex);         range.select();     }     textbox.focus();}</script><textarea cols="40" rows="10" id="target"><div>php</div><div>qqq</div></textarea><input type="button" onclick="foo()" value="Click"> 

热点排行