var getSelectionHTML = function() {
try {
var userSelection;
if (window.getSelection) {
// W3C Ranges
userSelection = window.getSelection();
// Get the range:
if (userSelection.getRangeAt)
var range = userSelection.getRangeAt(0);
else {
var range = document.createRange();
range.setStart(userSelection.anchorNode,
userSelection.anchorOffset);
range
.setEnd(userSelection.focusNode,
userSelection.focusOffset);
}
// And the HTML:
var clonedSelection = range.cloneContents();
var div = document.createElement('div');
div.appendChild(clonedSelection);
return div.innerHTML.replace(/<.>| /ig, '');
} else if (document.selection) {
// Explorer selection, return the HTML
userSelection = document.selection.createRange();
return userSelection.htmlText.replace(/<.>| /ig, '');
} else {
return '';
}
} catch (e) {
return '';
}
}
效果如下图