function getSelectedText() {
//document.designMode = 'on';
var userSelection, ta;
if (window.getSelection && document.activeElement){
if (document.activeElement.nodeName == "TEXTAREA" || (document.activeElement.nodeName == "INPUT" &&
document.activeElement.getAttribute("type").toLowerCase() == "text")){
ta = document.activeElement;
userSelection = ta.value.substring(ta.selectionStart,ta.selectionEnd);
} else {
userSelection = window.getSelection().getRangeAt(0);
}
return userSelection.toString();
} else {
//all browsers, except IE before version 9
if (document.getSelection){
userSelection = document.getSelection().getRangeAt(0);
return userSelection.toString();
}
// IE below version 9
else if (document.selection){
userSelection = document.selection.createRange();
return userSelection.text;
}
}
return "";
}
上面是我获取的函数,但是经过实验发现,可以得到 userSelection 对象,但是通过 toString 得到的文本却是空,请大家帮帮忙,多谢!!!
我的环境:ubuntu+chrome
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
win7下测试,没发现什么问题,
话说题主调用这个函数的时机是啥