textarea文本选择onselect事件初探
<html> <head> <style type='text/css'> textarea{ border:solid 1px green; width:200px; height:200px; } </style> <script language='javascript'> var sel_obj; function fun(obj) { //alert(obj.selected) sel_obj=document.selection.createRange(); if(!sel_obj.text){ alert('没有选择到文本!'); }else{ //alert(sel_obj.text); document.title=sel_obj.text; } } </script> </head> <body> <textarea onselect='fun(this);'>hello world hello world</textarea> </body> </html>
这里每选中一次文本,都要使用document.selection.createRange重新建立对象,以便使用text属性,获取选中的内容。
这个onselect事件,在div中并不存在。