1

I am not able to get the selection object's start and end offsets in Opera (v9.50) when the selection is collapsed (ie just point and click instead of highlighting text). 这是我的简单测试代码,在 FF 和 Safari 中有效,但在 Opera 中无效。

<html>
<head>

<script type="text/javascript">
    function showSelection()
    {
        userSelection = window.getSelection();
        alert('Start: '+userSelection.anchorOffset+'\nEnd: '+userSelection.focusOffset);
    }   
</script>

</head>

<body>

    <div onmouseup="showSelection();">
        <p>Lorem ipsum dolor <strong>sit amet</strong></p>
    </div>

</body>
</html>

当我选择一个文本时,它可以工作,但只是指向和单击总是将偏移量返回为 0。我错过了什么?提前致谢。

4

2 回答 2

1

Opera does not seem to make a selection when you click. I don't know what you're trying to achieve, but if you for instance want to get the selected text, Opera also implements 'document.selection' (probably to support otherwise IE only scripts) so you can do things like "document.selection.createRange().text" (not very helpful, because when nothing is selected it will return an empty string).

I think you're out of luck with this one.

于 2008-12-31T00:33:15.877 回答
1

我建议把这个放在 Opera 论坛上,肯定会有人(例如开发人员)可以给你一个明确的是或否。

歌剧论坛

于 2008-12-31T04:29:39.053 回答