取得選取中的文字

在文章中取得選取中的文字,然後將文字post出去進行其他動作,已經是常見的應用。
以下是選取文字的javascript code:

function checkhl(){
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}else if(document.getSelection){
txt = document.getSelection();
}else if(document.selection){
txt = document.selection.createRange().text;
}
if(txt!='')
{
document.getElementById('ta').value=txt;
}else{
document.getElementById('ta').value='';
}
}

沒有留言: