日期:2014-05-16  浏览次数:20332 次

JAVAScript document.selection.createRange方法

document.selection.createRange() 方法根据当前选择文字返回 TextRange 对象,或根据选择控件返回 ControlRange 对象。

配合 execCommand,在 HTML 编辑器中可以完成很多事情,比如文字加粗、斜体、复制、粘贴、创建超链接等。

?

实例一:

<textarea cols=50 rows=15>

测试内容!</textarea>

<input type=button value=选择字后点击我看看 onclick=alert(document.selection.createRange().text)>

?

实例二:

<body>

<textarea name="textfield" cols="50" rows="6">就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字

哪位老大能解决的呀?请多多帮忙!!!谢谢

</textarea>

<input type="button" value="showSelection" onclick="alert(document.selection.createRange().text)">

<input type="button" value="showclear" onclick="alert(document.selection.clear().text)">

<input type="button" value="showtype" onclick="alert(document.selection.type)">

<textarea name="textfield" cols="50" rows="6" onselect="alert(document.selection.createRange().text)">就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字

哪位老大能解决的呀?请多多帮忙!!!谢谢

</textarea>

</body>

?

实例三:选中Input中的文本

?

<SCRIPT LANGUAGE="JavaScript">

<!--

function test2()

{

var t=document.getElementById("test")

var o=t.createTextRange()

alert(o.text)

o.moveStart("character",2)

alert(o.text)

o.select()

}

//-->

</SCRIPT>

<input type='text' id='test' name='test'><input type=button onclick='test2()' value='test' name='test3'>