document.selection window.getSelection()


IE9以下支持:document.selection    
IE9、Firefox、Safari、Chrome和Opera支持:window.getSelection()

 

屏幕取詞

function getWord(){
    var word = window.getSelection?window.getSelection():document.selection.createRange().text;
    alert( word )   
}
document.body.addEventListener("click", getWord, false);
1

 

. 移除選中的內容

function removeWord(){
    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
document.body.addEventListener("click", removeWord, false);

 

小實例http://www.jb51.net/article/2835.htm

 

http://www.jb51.net/article/23421.htm

<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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM