<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>varg=document>varg=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>var r = document.selection.createRange(); <BR>if(o.createTextRange().inRange(r)) <BR>alert(r.text); <BR>} <BR></script> </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> <br>ababababababa <br><input value="click =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> </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> <br><script language="javascript"> <BR>function setFoc() <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()"> </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><br> <br><script language="javascript"> <BR>function getPos(obj){ <BR>obj.focus(); <BR>var s=document.selection.createRange(); <BR>s.setEndPointPoint ("StartToStart",obj.createTextRange()) <BR>alert(s.text.length); <BR>} <BR></script> <br><input type="text" id="txt1" value="1234567890"> </div> <input type="button" value="得到光標位置" onclick="getPos(txt1)"> <br><br> <div class="codetitle">控制input框內光標位置<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><br> <br><script language="javascript"> <BR>function setPos(num) <🎜(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> </select> </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> <br>function sel(obj,num) <br>{ <br>varg=obj.createTextRange() <br>var sel = rng.duplicate(); 🎜>sel.moveStart("字元", num); <br>sel.setEndPoint("EndToStart", rng); <br>sel.select(); <br>} <br>腳本> <br> <br><select onchange="sel(text1,this.value)"> <br>0 <br>1選項> <br>2選項> <br>3選項> <br>4選項> <br>5選項> <br>6選項> <br>7選項> <br>選擇> <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><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><br> </div>