简体   繁体   English

如何使用“window.getSelection()”获取HTML

[英]how to get HTML with “window.getSelection()”

My code: 我的代码:

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

what to write at "//code" that I could get the same htmlText in chrome or FF ? 在“// code”处写什么我可以在chrome或FF中获得相同的htmlText?

http://jsfiddle.net/kyP83/ 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM