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>