不过这东西貌似只能在ie下运行,如果是其他浏览器,得另想办法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>文本被选中</title>
<script type="text/javascript">
function selectItem1(){
var mydiv = document.getElementById("myDiv");
var range= document.body.createTextRange();
range.moveToElementText(mydiv)
range.moveEnd('character',-1);
range.moveStart('character',3);
range.select();
}
function selectItem2(){
var obj = document.getElementsByName("textRange")[0];
var range = obj.createTextRange()
range.moveStart("character",10);
range.moveEnd("character",-10);
range.select();
}
</script>
</head>
<body>
<div id="myDiv" οnclick="selectItem()">
再过二十天左右就要面临高考了,1321321有许多考222生们目前正在紧张的复习当中。但是在北京出现
</div>
<input type="button" value="选中上面的文本" οnclick="selectItem1()" />
</br>
<input type="text" name="textRange" size="100" value="要检查您的网络连接,请单击工具菜单,然后单击 Internet 选项。">
</br>
<input type="button" value="选中上面的文本" οnclick="selectItem2()" title="" />
<table border="1">
<tr height="100px">
<td align="center" style="text-align:center" >aaa<img style="margin-left:100px" src="../../Public/Pictures/Sample Pictures/Hydrangeas.jpg" height="100px" width="200px"/><font style="font-size: 18px; font-style: inherit"><b>@33</b></font></td>
</tr>
</table>
</body>
</html>