这就是我想出来的。经过IE,Chrome,Firefox,Safari,Opera测试。不返回空字符串。function getSelected() {
var text = "";
if (window.getSelection && window.getSelection().toString()
&& $(window.getSelection()).attr('type') != "Caret") {
text = window.getSelection();
return text;
}
else if (document.getSelection && document.getSelection().toString()
&& $(document.getSelection()).attr('type') != "Caret") {
text = document.getSelection();
return text;
}
else {
var selection = document.selection && document.selection.createRange();
if (!(typeof selection === "undefined")
&& selection.text && selection.text.toString()) {
text = selection.text;
return text;
}
}
return false;}