您的位置:首页 > 其它

textarea文本选择onselect事件初探

2012-02-29 22:06 302 查看
<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中并不存在。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: