處理文本部分內(nèi)容的TextRange對象應用實例
因用戶要求方與TextRange對象結緣,用于處理JavaScript對象文本部分內(nèi)容的一個對象。
TextRange是用來表現(xiàn)HTML元素中文字的對象,雖然我們平時不太常用這個對象,可是它卻在IE4.0中就已提供了。不過TextRange提供的調(diào)用方法卻都比較晦澀,那么我們能拿它做些什么呢?
TextRange的傳統(tǒng)用途是對用戶在Web頁上用鼠標圈選的文字內(nèi)容的操作,比如變化、刪除、新增等。但其經(jīng)典的用途卻是,在Web頁面中查找文字(這個比較簡單)和獲取輸入框光標的位置。其中后者又有可以衍生出很多更有用的用途,比如:限制輸入的MaskTextBox,其核心技術點就是獲取輸入框的光標位置,然后使用正則表達式判斷輸入內(nèi)容。還有我后面會介紹的"使用方向鍵在輸入框矩陣中自然的導航",核心技術點也是獲取輸入框中的光標位置。
獲取輸入框中的光標位置的整個代碼其實很短,只是這些對象和方法不太常用而已。
Js代碼
<span style="font-size: medium;"><script language="javascript"> function GetCursorPsn(txb) { var slct = document.selection; var rng = slct.createRange(); txb.select(); rng.setEndPoint("StartToStart", slct.createRange()); var psn = rng.text.length; rng.collapse(false); rng.select(); return psn; } </script></span>
這里說一下使用這個GetCursorPsn()方法后,會給輸入框操作帶來的副作用。
對于輸入框
Html代碼
<span style="font-size: medium;"><input type="text" onkeydown="GetCursorPsn(this)"></span>
它將不能再使用Shift+左右這兩個方向鍵來選擇文本;對于
Html代碼
<span style="font-size: medium;"><textarea onkeydown="GetCursorPsn(this)"></textarea></span>
,將不能再使用Shift+上下左右四個方向鍵來選擇文本。因為代碼在獲取了當前光標到文本的startPoint后,調(diào)用rng.collapse(false );會 改變文本筐內(nèi)文本的EditPoint。
1、滿足用戶要求代碼片段,使用上下左右四個鍵實現(xiàn)文本框的跳轉,同時選擇其文本框內(nèi)容,從而方便用戶修改,代碼如下:
Js代碼
<span style="font-size: medium;">var range = $currentTextfield.createTextRange();//$currentTextfield為jQuery對象 range.moveStart('character',0); range.select();</span>
以下是舶來的一片個人感覺還算不錯的關于TextRange的文章:
Html代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> body { font-size:12px; } #show { background-color:#CCFF99; } </style> </head> <body> <textarea id="content" cols="30" rows="10"> 河中魚類離奇死亡,下游居民頻染怪病,沿岸植物不斷變異,是殘留農(nóng)藥?還是生化攻擊?敬請關注今晚CCTV-10《科學探索》,即將播出的專題節(jié)目:《神秘的河邊洗腳人--中國男足》 </textarea> <button id="btn">獲取選中值</button> <div id="show"></div> <script> String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); } /* 方法一 FF下有點問題 */ function getSelectText() { try{ // IE: document.selection.createRange() W3C:window.getSelection() var selectText = (document.selection && document.selection.createRange )? document.selection.createRange().text : window.getSelection().toString(); if(selectText != null && selectText.trim() != ""){ return selectText; } }catch(err){} } /* 方法二 */ function getSelectText2(id) { var t = document.getElementById(id); if(window.getSelection) { if(t.selectionStart != undefined && t.selectionEnd != undefined) { return t.value.substring(t.selectionStart, t.selectionEnd); } else { return ""; } } else { return document.selection.createRange().text; } } document.getElementById('btn').onclick = function() { document.getElementById('show').innerHTML = getSelectText2('content'); } </script> </body> </html>
相關文章
js入門之Function函數(shù)的使用方法【新手必看】
本篇文章主要介紹js Function函數(shù)的使用方法,應該對初學Js的朋友們會有所幫助,下面就隨小編一起來看下吧2016-11-11