好的,这是我的代码:
function ShowSelection(){ var textComponent = document.getElementById('Editor'); var selectedText; if (textComponent.selectionStart !== undefined) {// Standards Compliant Version var startPos = textComponent.selectionStart; var endPos = textComponent.selectionEnd; selectedText = textComponent.value.substring(startPos, endPos); } else if (document.selection !== undefined) {// IE Version textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; } alert("You selected: " + selectedText);}问题是,尽管我为IE提供的代码在许多站点上都有提供,但我无法使其在我当前系统上的IE6副本上正常工作。也许它将对您有用,这就是为什么我给它。
您寻找的技巧可能是.focus()调用,它可以将焦点返回给textarea以便重新激活选择。
[更新]我通过onKeyDown事件得到了正确的结果(选择内容):
document.onkeydown = function (e) { ShowSelection(); }所以代码是正确的。同样,问题是单击按钮即可获得选择…我继续搜索。
[更新]使用
li标记绘制的按钮没有成功,因为当我们单击它时,IE会取消选择先前的选择。上面的代码使用一个简单的
input按钮即可,但是…



