document.selection.createRange方法與執行個體_javascript技巧

來源:互聯網
上載者:User
配合 execCommand,在 HTML 編輯程式中很有用,比如:文字加粗、斜體、複製、粘貼、建立超連結等。

<textarea cols=50 rows=15> 哈哈。我們都是新生來得。大家都來相互協助呀。這樣我們才能進步,我們才能賺大錢!</textarea> <input type=button value=選擇字後點擊我看看 onclick=alert(document.selection.createRange().text)> </form>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]


這個不錯
<body> <p> </p> <p> <textarea name="textfield" cols="50" rows="6">就是現在文本域裡有一段文字,當你選種其中幾個字後點擊一個按鈕或者連結會彈出一個對話方塊,對話方塊的資訊就是你選中的文字 哪位老大能解決的呀?請多多幫忙!!!謝謝 </textarea> </p> <p> <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)"> </p> <p> <textarea name="textfield" cols="50" rows="6" onselect="alert(document.selection.createRange().text)">就是現在文本域裡有一段文字,當你選種其中幾個字後點擊一個按鈕或者連結會彈出一個對話方塊,對話方塊的資訊就是你選中的文字 哪位老大能解決的呀?請多多幫忙!!!謝謝 </textarea> </p> </body>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
 


執行個體三:選中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" >選中我,點擊加粗</textarea>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

執行個體四: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>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.