1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
function getSelectedHtml(){
var selectedHtml = "" ;
var documentFragment = null ;
try {
if (window.getSelection){
documentFragment = window.getSelection().getRangeAt(0).cloneContents();
} else if (document.selection){
documentFragment = document.selection.createRange().HtmlText;
}
for ( var i=0;i<documentFragment.childNodes.length;i++){
var childNode = documentFragment.childNodes[i];
if (childNode.nodeType==3){ // Text 节点
selectedHtml+=childNode.nodeValue;
} else {
var nodeHtml = childNode.outerHTML;
selectedHtml+=nodeHtml;
}
}
} catch (err){
}
return selectedHtml;
} |
本文转自 antlove 51CTO博客,原文链接:http://blog.51cto.com/antlove/1728155