亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

處理文本部分內(nèi)容的TextRange對(duì)象應(yīng)用實(shí)例

 更新時(shí)間:2014年07月29日 09:48:49   投稿:whsnow  
TextRange是用來(lái)表現(xiàn)HTML元素中文字的對(duì)象,是一個(gè)用于處理JavaScript對(duì)象文本部分內(nèi)容的一個(gè)對(duì)象

因用戶(hù)要求方與TextRange對(duì)象結(jié)緣,用于處理JavaScript對(duì)象文本部分內(nèi)容的一個(gè)對(duì)象。

TextRange是用來(lái)表現(xiàn)HTML元素中文字的對(duì)象,雖然我們平時(shí)不太常用這個(gè)對(duì)象,可是它卻在IE4.0中就已提供了。不過(guò)TextRange提供的調(diào)用方法卻都比較晦澀,那么我們能拿它做些什么呢?
TextRange的傳統(tǒng)用途是對(duì)用戶(hù)在Web頁(yè)上用鼠標(biāo)圈選的文字內(nèi)容的操作,比如變化、刪除、新增等。但其經(jīng)典的用途卻是,在Web頁(yè)面中查找文字(這個(gè)比較簡(jiǎn)單)和獲取輸入框光標(biāo)的位置。其中后者又有可以衍生出很多更有用的用途,比如:限制輸入的MaskTextBox,其核心技術(shù)點(diǎn)就是獲取輸入框的光標(biāo)位置,然后使用正則表達(dá)式判斷輸入內(nèi)容。還有我后面會(huì)介紹的"使用方向鍵在輸入框矩陣中自然的導(dǎo)航",核心技術(shù)點(diǎn)也是獲取輸入框中的光標(biāo)位置。

獲取輸入框中的光標(biāo)位置的整個(gè)代碼其實(shí)很短,只是這些對(duì)象和方法不太常用而已。

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>

這里說(shuō)一下使用這個(gè)GetCursorPsn()方法后,會(huì)給輸入框操作帶來(lái)的副作用。

對(duì)于輸入框

Html代碼

<span style="font-size: medium;"><input type="text" onkeydown="GetCursorPsn(this)"></span>

它將不能再使用Shift+左右這兩個(gè)方向鍵來(lái)選擇文本;對(duì)于

Html代碼

<span style="font-size: medium;"><textarea onkeydown="GetCursorPsn(this)"></textarea></span>

,將不能再使用Shift+上下左右四個(gè)方向鍵來(lái)選擇文本。因?yàn)榇a在獲取了當(dāng)前光標(biāo)到文本的startPoint后,調(diào)用rng.collapse(false );會(huì) 改變文本筐內(nèi)文本的EditPoint。

1、滿(mǎn)足用戶(hù)要求代碼片段,使用上下左右四個(gè)鍵實(shí)現(xiàn)文本框的跳轉(zhuǎn),同時(shí)選擇其文本框內(nèi)容,從而方便用戶(hù)修改,代碼如下:

Js代碼

<span style="font-size: medium;">var range = $currentTextfield.createTextRange();//$currentTextfield為jQuery對(duì)象 

range.moveStart('character',0); 

range.select();</span>

以下是舶來(lái)的一片個(gè)人感覺(jué)還算不錯(cuò)的關(guān)于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"> 

河中魚(yú)類(lèi)離奇死亡,下游居民頻染怪病,沿岸植物不斷變異,是殘留農(nóng)藥?還是生化攻擊?敬請(qǐng)關(guān)注今晚CCTV-10《科學(xué)探索》,即將播出的專(zhuān)題節(jié)目:《神秘的河邊洗腳人--中國(guó)男足》 

</textarea> 


<button id="btn">獲取選中值</button> 

<div id="show"></div> 

<script> 

String.prototype.trim = function() { 

return this.replace(/^\s+|\s+$/g, ""); 

} 

/* 方法一 FF下有點(diǎn)問(wèn)題 */ 

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>

相關(guān)文章

最新評(píng)論