<div class="codetitle"> <span><a style="CURSOR: pointer" data="51432" class="copybut" id="copybut51432" onclick="doCopy('code51432')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code51432"> <br><script language="javascript"> <br>function test() <br>{ <br>var rng=document.body.createTextRange(); <br>alert(rng.text) <br>} <br>function test1() <br>{ <br>var rng=document.body.createTextRange(); <br>alert(rng.htmlText) <br>} <br></script> <br><input type="button" onclick="test()" value="text"> <br><input type="button" onclick="test1()" value="htmlText"> <br> </div> <br>获取指定文本框中的选中的文字:只响应第一个文本框 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="43146" class="copybut" id="copybut43146" onclick="doCopy('code43146')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code43146"> <br><input id="inp1" type="text" value="1234567890"> <br><input id="inp2" type="text" value="9876543210"> <br><input type="button" onclick="test()" value="确定"> <br><script language="javascript"> <br>function test() <br>{ <br>var o=document.getElementById("inp1") <br>var r = document.selection.createRange(); <br>if(o.createTextRange().inRange(r)) <br>alert(r.text); <br>} <br></script> <br> </div> <br>页面文本倒序查找 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="21120" class="copybut" id="copybut21120" onclick="doCopy('code21120')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code21120"> <br>abababababababa <br><input value="倒序查找a" onclick=myfindtext("a") type="button"> <br><script language ='javascript'> <br>var rng = document.body.createTextRange(); <br>function myfindtext(text) <br>{ <br>rng.collapse(false); <br>if(rng.findText(text,-1,1)) <br>{ <br>rng.select(); <br>rng.collapse(true); <br>}else <br>{alert("end");} <br>} <br></script> <br> </div> <br>聚焦控件后把光标放到最后 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="60641" class="copybut" id="copybut60641" onclick="doCopy('code60641')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code60641"> <br><script language="javascript"> <br>function setFocus() <br>{ <br>var obj = event.srcElement; <br>var txt =obj.createTextRange(); <br>txt.moveStart('character',obj.value.length); <br>txt.collapse(true); <br>txt.select(); <br>} <br></script> <br><input type="text" value="http://toto369.net" onfocus="setFocus()"> <br> </div> <br>得到文本框内光标位置 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="42078" class="copybut" id="copybut42078" onclick="doCopy('code42078')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code42078"> <br><script language="javascript"> <br>function getPos(obj){ <br>obj.focus(); <br>var s=document.selection.createRange(); <br>s.setEndPoint("StartToStart",obj.createTextRange()) <br>alert(s.text.length); <br>} <br></script> <br><input type="text" id="txt1" value="1234567890"> <br><input type="button" value="得到光标位置" onclick=getPos(txt1)> <br> </div> <br>控制input框内光标位置 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="24324" class="copybut" id="copybut24324" onclick="doCopy('code24324')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code24324"> <br><script language="javascript"> <br>function setPos(num) <br>{ <br>text1.focus(); <br>var e =document.getElementById("text5"); <br>var r =e.createTextRange(); <br>r.moveStart('character',num); <br>r.collapse(true); <br>r.select(); <br>} <br></script> <br><input type="text" id="text5" value="1234567890"> <br><select onchange="setPos(this.selectedIndex)"> <br><option value="0">0</option> <br><option value="1">1</option> <br><option value="2">2</option> <br><option value="3">3</option> <br><option value="4">4</option> <br><option value="5">5</option> <br><option value="6">6</option> <br><option value="7">7</option> <br></select> <br> </div> <br>选中文本框中的一段文字 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="51027" class="copybut" id="copybut51027" onclick="doCopy('code51027')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code51027"> <br><脚本语言=javascript> <br>function sel(obj,num) <br>{ <br>var rng=obj.createTextRange() <br>var sel = rng.duplicate(); <br>sel.moveStart("字符", num); <br>sel.setEndPoint("EndToStart", rng); <br>sel.select(); <br>} <br></脚本> <br> <br><select onchange="sel(text1,this.value)"> <br><选项值=“0”>0</选项> <br>1选项> <br>2选项> <br>3选项> <br>4选项> <br>5选项> <br>6选项> <br>7选项> <br></选择> <br></select> </div> <br>控制文本框内部的移动<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="70917" class="copybut" id="copybut70917" onclick="doCopy('code70917')"><u>复制代码</u></a></span>代码如下:</div> <div class="codebody" id="code70917"> <br><输入类型=“按钮”值=“<” onclick=开始(-1)> <BR><input id="demo" value="这里是文字"> <br>” onclick=开始(1)> <br> <br>函数 go(n){ <br>demo.focus(); <br>with(document.selection.createRange()) <br>{ <br>moveStart("character",n); <br>崩溃(); <br>选择(); <br>} <br>} <br></脚本> <br> </div>