0

我的代码:

<p id="p">
    123<span>abc</span>456
</p>
<script>
    document.getElementById("p").onmouseup=function(){
         if (window.getSelection) {
             //code
         }
         else if (document.selection) {   
             alert(document.selection.createRange().htmlText)  //IE6 7 8
         }
    }
</script>

在“//code”处写什么,我可以在 chrome 或 FF 中获得相同的 htmlText?

4

1 回答 1

2

http://jsfiddle.net/kyP83/

document.getElementsByTagName('p')[0].onmouseup = function() {
    if (typeof window.getSelection === 'function') {
        //code
        var selObj = window.getSelection(); 
        alert(selObj);
        var selRange = selObj.getRangeAt(0);             
    } else if (document.selection) {   
        alert(document.selection.createRange().htmlText)  //IE6 7 8
    }
}
于 2013-01-26T08:25:54.317 回答