js获取网页上选中的部分,包含html代码
function getSelectedContents(){
if (window.getSelection) { //chrome,firefox,opera
var range=window.getSelection().getRangeAt(0);
var container = document.createElement('div');
container.appendChild(range.cloneContents());
return container.innerHTML;
//return window.getSelection(); //只复制文本
}
else if (document.getSelection) { //其他
var range=window.getSelection().getRangeAt(0);
var container = document.createElement('div');
container.appendChild(range.cloneContents());
return container.innerHTML;
//return document.getSelection(); //只复制文本
}
else if (document.selection) { //IE特有的
return document.selection.createRange().htmlText;
//return document.selection.createRange().text; //只复制文本
}
}
转载自:http://www.cnblogs.com/top5/archive/2012/06/20/2555711.html