如何找到光标所在的单词

发布于 2024-07-17 06:23:46 字数 778 浏览 1 评论 0原文

IE中如何检测光标所在的单词? 我尝试过使用这段代码:

window.setInterval(function () {
    var range = document.selection.createRange();
    range.expand('word');
    var wort = range.text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    document.getElementById("ausgabe").innerHTML = wort;
}, 100)
<textarea id="ta" rows="10" cols="40">At vero eos et accu-samus et iusto? Odio, dignissimos. ducimus qui bländitiis praeséntium voluptatèm deleniti atque corrupti quos</textarea>
<p>[<span id="ausgabe"></span>]</p>

但是当我将光标设置在文本区域的开头和结尾时,就会出现问题。 它给了我完整的文本。 我该如何解决这个问题?

How can I detect the word on which the cursor is located in IE?
I have tried with this code :

window.setInterval(function () {
    var range = document.selection.createRange();
    range.expand('word');
    var wort = range.text.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    document.getElementById("ausgabe").innerHTML = wort;
}, 100)
<textarea id="ta" rows="10" cols="40">At vero eos et accu-samus et iusto? Odio, dignissimos. ducimus qui bländitiis praeséntium voluptatèm deleniti atque corrupti quos</textarea>
<p>[<span id="ausgabe"></span>]</p>

But the problem occurs when I set the cursor at the beginning and end of the Textarea. it gives me the complete text. How can I fix that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-07-24 06:23:46

我确信有更好的方法来解决这个问题,所以我希望其他人也能阐明这个问题。 该解决方案使用一些试验和错误来移动范围 - 如果出现问题,它会恢复到原始状态。

我希望这有帮助!

这是代码:

<html>
<body>
<script>
    window.setInterval(
        function () {
            var selection = document.selection;
            var range = document.selection.createRange();

            var parentEl = range.parentElement();

            // Make a duplicate range to revert to if things go wrong.
            range2 = range.duplicate();

            range.moveStart('character', 1);
            if (range.parentElement() != parentEl) {
                // We've left the original parent (which is bad), so revert to the original range. We're probably at the end of the textarea.
                range = range2.duplicate();
            }
            range.moveStart('word', -1);

            // Make a new duplicate range to revert to.
            range2 = range.duplicate();

            // Move the end of the range one backwards, then forward to the end of the word.
            range.moveEnd('character', -1);
            range.moveEnd('word', 1);
            if (range.parentElement() != parentEl) {
                range = range2.duplicate();
                while (range2.parentElement() == parentEl) {
                    range.moveEnd('character', 1);
                    range2 = range.duplicate();
                }
            }

            var wort = range.text.replace(/^\s\s*$/, '').replace(/\s\s*$/, '');
            document.getElementById("ausgabe").innerHTML = wort;
        }, 100
    )
</script>

    <textarea id="ta" rows="10" cols="40">At vero eos et accu-samus et iusto? Odio, dignissimos. ducimus qui bländitiis praeséntium voluptatèm deleniti atque corrupti quos</textarea>

<p>[<span id="ausgabe"></span>]</p>
</body>
</html>

I'm sure there is a better way to solve this, so I hope someone else will also shed some light on this issue. This solution uses some trial and error for moving the range about - if things so wrong, it is reverted to the original state.

I hope this helps!

Here is the code:

<html>
<body>
<script>
    window.setInterval(
        function () {
            var selection = document.selection;
            var range = document.selection.createRange();

            var parentEl = range.parentElement();

            // Make a duplicate range to revert to if things go wrong.
            range2 = range.duplicate();

            range.moveStart('character', 1);
            if (range.parentElement() != parentEl) {
                // We've left the original parent (which is bad), so revert to the original range. We're probably at the end of the textarea.
                range = range2.duplicate();
            }
            range.moveStart('word', -1);

            // Make a new duplicate range to revert to.
            range2 = range.duplicate();

            // Move the end of the range one backwards, then forward to the end of the word.
            range.moveEnd('character', -1);
            range.moveEnd('word', 1);
            if (range.parentElement() != parentEl) {
                range = range2.duplicate();
                while (range2.parentElement() == parentEl) {
                    range.moveEnd('character', 1);
                    range2 = range.duplicate();
                }
            }

            var wort = range.text.replace(/^\s\s*$/, '').replace(/\s\s*$/, '');
            document.getElementById("ausgabe").innerHTML = wort;
        }, 100
    )
</script>

    <textarea id="ta" rows="10" cols="40">At vero eos et accu-samus et iusto? Odio, dignissimos. ducimus qui bländitiis praeséntium voluptatèm deleniti atque corrupti quos</textarea>

<p>[<span id="ausgabe"></span>]</p>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文