TextRange Object--------------------------------------------------------------------------------Represents text in an HTML element. createTextRange Method--------------------------------------------------------------------------------Creates a TextRange object for the element.SyntaxoTextRange = object.createTextRange() Return ValueReturns a TextRange object if successful, or null otherwise. ResUse a text range to examine and modify the text within an object.ExamplesThis example uses the createTextRange method to create a text range for the document, and then uses the text range to display all the text and HTML tags in the document.<SCRIPT LANGUAGE="JScript"> var rng = document.body.createTextRange( ); if (rng!=null) { alert(rng.htmlText); } </SCRIPT> This example uses the createTextRange method to create a text range for the first button element in the document, and then uses the text range to change the text in the button.<SCRIPT LANGUAGE="JScript"> var coll = document.all.tags("BUTTON"); if (coll!=null && coll.length>0) { var rng = coll[0].createTextRange(); rng.text = "Clicked"; } </SCRIPT> Standards InformationThere is no public standard that applies to this method. 我没看到textrange有caretPos属性 messge是个元素或对象,直接message.caretpos=1
解释一下:你的光标插入位置
自己定义的吧
什么是TextRange?什么用?to yjgx007
它返回的数据结构是什么类型的?to kingdomzhf
好像不是!!!怎么定义呀?呵呵赐教!谢谢!
<HEAD>
<SCRIPT>
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
}
else
textEl.value = text; }
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="aForm">
<TEXTAREA NAME="aTextArea" ROWS="5" COLS="80" WRAP="soft" ONSELECT="storeCaret(this);" ONCLICK="storeCaret(this);" ONKEYUP="storeCaret(this);"> 这是例子,你可以在这里添加文字、插入文字。 </TEXTAREA>
<BR>
<INPUT TYPE="text" NAME="aText" SIZE="80" VALUE="我要在光标处插入这些文字"><BR>
<INPUT TYPE="button" VALUE="我要在光标处插入上面文本框里输入的文字!" ONCLICK="insertAtCaret(this.form.aTextArea, this.form.aText.value);">
</FORM>
</BODY>
</HTML>
TextRange Object--------------------------------------------------------------------------------Represents text in an HTML element.
createTextRange Method--------------------------------------------------------------------------------Creates a TextRange object for the element.SyntaxoTextRange = object.createTextRange()
Return ValueReturns a TextRange object if successful, or null otherwise. ResUse a text range to examine and modify the text within an object.ExamplesThis example uses the createTextRange method to create a text range for the document, and then uses the text range to display all the text and HTML tags in the document.<SCRIPT LANGUAGE="JScript">
var rng = document.body.createTextRange( );
if (rng!=null) {
alert(rng.htmlText);
}
</SCRIPT>
This example uses the createTextRange method to create a text range for the first button element in the document, and then uses the text range to change the text in the button.<SCRIPT LANGUAGE="JScript">
var coll = document.all.tags("BUTTON");
if (coll!=null && coll.length>0) {
var rng = coll[0].createTextRange();
rng.text = "Clicked";
}
</SCRIPT>
Standards InformationThere is no public standard that applies to this method. 我没看到textrange有caretPos属性
messge是个元素或对象,直接message.caretpos=1