document.selection

來源:互聯網
上載者:User
關鍵字: js

IE:document.selection   FireFox:window.getSelection()   document.selection只有IE支援,window.getSelection()也只有FireFox和   Safari支援,都不是標準文法。
selection   對象 
  
  -------------------------------------------------------------------------------- 
  
  代表了當前啟用選中區,即高亮文字區塊,和/或文檔中使用者可執行某些操作的其它元素。 
  selection   對象的典型用途是作為使用者的輸入,以便識別正在對文檔的哪一部分正在處理,或者作為某一操作的結果輸出給使用者。 
  
  使用者和指令碼都可以建立選中區。使用者建立選中區的辦法是拖曳文檔的一部分。指令碼建立選中區的辦法是在文本地區或類似對象上調用   select   方法。要擷取當前選中區,請對   document   對象應用   selection   關鍵字。要對選中區執行操作,請先用   createRange   方法從選中區建立一個文本地區對象。 
  
  一個文檔同一時間只能有一個選中區。選中區的類型決定了其中為空白或者包含文本和/或元素塊。儘管空的選中區不包含任何內容,你仍然可以用它作為文檔中的位置標誌。

一個簡單的例子

<html><head><title>document.selection.createRange例子</title></head><body>

<div>請選中這裡的部分文字。</div><div><input type="button" value="加粗" onclick="javascript :Bold();" /></div>

<script  language="javascript">

function Bold(){

var bo = document.selection.createRange();

bo.execCommand("Bold");

}</script>

</body></html>

document.selection.createRange() 根據當前文字選擇返回 TextRange 對象,或根據控制項選擇返回 ControlRange 對象。

配合 execCommand,在 HTML 編輯程式中很有用,比如:文字加粗、斜體、複製、粘貼、建立超連結等。

執行個體一:

<textarea cols=50 rows=15>
哈哈。我們都是新生來得。大家都來相互協助呀。這樣我們才能進步,我們才能賺大錢!</textarea>
<input type=button value=選擇字後點擊我看看 onclick=alert(document.selection.createRange().text)>
</form>

執行個體二:

<body>
<textarea name="textfield" cols="50" rows="6">就是現在文本域裡有一段文字,當你選種其中幾個字後點擊一個按鈕或者連結會彈出一個對話方塊,對話方塊的資訊就是你選中的文字
哪位老大能解決的呀?請多多幫忙!!!謝謝
</textarea>
<input type="button" value="showSelection" onclick="alert(document.selection.createRange().text)">
<input type="button" value="showclear" onclick="alert(document.selection.clear().text)">
<input type="button" value="showtype" onclick="alert(document.selection.type)">
<textarea name="textfield" cols="50" rows="6" onselect="alert(document.selection.createRange().text)">就是現在文本域裡有一段文字,當你選種其中幾個字後點擊一個按鈕或者連結會彈出一個對話方塊,對話方塊的資訊就是你選中的文字
哪位老大能解決的呀?請多多幫忙!!!謝謝
</textarea>

</body>

執行個體三:選中Input中的文本

<SCRIPT LANGUAGE="JavaScript">
<!--
function test2()
{
var t=document.getElementById("test")
var o=t.createTextRange()
alert(o.text)
o.moveStart("character",2)
alert(o.text)
o.select()
}
//-->
</SCRIPT>
<input type='text' id='test' name='test'><input type=button onclick='test2()' value='test' name='test3'>
對textarea中的內容,進行選中後,加效果
<script language="JavaScript">
<!--
function bold(){
Qr=document.selection.createRange().text;
if(!Qr || document.selection.createRange().parentElement().name!='description')
{
txt=prompt('Text to be made BOLD.','');
if(txt!=null && txt!='') document.form1.description.value+=''+txt+'';
}
else{
document.selection.createRange().text=''+document.selection.createRange().text+'';
document.selection.empty();
}
}
//-->
</script>
<input type="button" value="加粗" onclick="bold();" />
<textarea name="description" style="width: 436px; height: 296px">選中我,點擊加粗</textarea>
執行個體四:javascript捕獲到選中的網頁中的純文字內容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>滑鼠取詞</title>
<script>
function getSel()
{
var t=window.getSelection?window.getSelection():(document.getSelection?document.getSelection():(document.selection?document.selection.createRange().text:""))
document.forms[0].selectedtext.value = t;
}
</script></head>
<body onmouseup="getSel()">
<form>
<textarea name="selectedtext" rows="5" cols="50"></textarea>
</form>
以上的代碼可以捕獲到選中的網頁中的純文字內容(不含HTML標籤)
如果想獲得包含html的內容,將document.selection.createRange().text改成document.selection.createRange().htmlText
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.