将选定的文本扩展为整个单词

发布于 2024-12-10 20:15:28 字数 380 浏览 0 评论 0原文

在 IE 中,我可以执行以下操作:

var rng = document.selection.createRange();
rng.expand("word");
txt = rng.text;

How do I do I do the same inside of IE?

使用 getSelection 选择整个单词 建议使用 window.getSelection().modify(),但我不想修改选择。

In IE I can do this:

var rng = document.selection.createRange();
rng.expand("word");
txt = rng.text;

How do I do the equivalent outside of IE?

Select whole word with getSelection
suggested using window.getSelection().modify(), but I don't want to modify the selection.

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

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

发布评论

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

评论(2

十秒萌定你 2024-12-17 20:15:28

我已经接受了亚历山大的答案,因为它应该跨越元素边界。我不需要这个,所以我实际使用的解决方案如下。

function GetSelectedText()
{
    var t = '';
    if (window.getSelection) // FF4 with one tab open?
    {
        var rng = window.getSelection().getRangeAt(0);
        expandtoword(rng);
        t = rng.toString();
    }
    else if (document.getSelection) // FF4 with multiple tabs open?
    {
        var rng = document.getSelection().getRangeAt(0);
        expandtoword(rng);
        t = rng.toString();
    }
    else if (document.selection) // IE8
    {
        var rng = document.selection.createRange();
        // expand range to enclose any word partially enclosed in it
        rng.expand("word");
        t = rng.text;
    }

    // convert newline chars to spaces, collapse whitespace, and trim non-word chars
    return t.replace(/\r?\n/g, " ").replace(/\s+/g, " ").replace(/^\W+|\W+$/g, '');
}

// expand FF range to enclose any word partially enclosed in it
function expandtoword(range)
{
    if (range.collapsed)
    {
        return;
    }

    while (range.startOffset > 0 && range.toString()[0].match(/\w/))
    {
        range.setStart(range.startContainer, range.startOffset - 1);
    }

    while (range.endOffset < range.endContainer.length && range.toString()[range.toString().length - 1].match(/\w/))
    {
        range.setEnd(range.endContainer, range.endOffset + 1);
    }
}

I've accepted Alexander's answer as it should work across element boundaries. I didn't need this, so the solution I actually used is below.

function GetSelectedText()
{
    var t = '';
    if (window.getSelection) // FF4 with one tab open?
    {
        var rng = window.getSelection().getRangeAt(0);
        expandtoword(rng);
        t = rng.toString();
    }
    else if (document.getSelection) // FF4 with multiple tabs open?
    {
        var rng = document.getSelection().getRangeAt(0);
        expandtoword(rng);
        t = rng.toString();
    }
    else if (document.selection) // IE8
    {
        var rng = document.selection.createRange();
        // expand range to enclose any word partially enclosed in it
        rng.expand("word");
        t = rng.text;
    }

    // convert newline chars to spaces, collapse whitespace, and trim non-word chars
    return t.replace(/\r?\n/g, " ").replace(/\s+/g, " ").replace(/^\W+|\W+$/g, '');
}

// expand FF range to enclose any word partially enclosed in it
function expandtoword(range)
{
    if (range.collapsed)
    {
        return;
    }

    while (range.startOffset > 0 && range.toString()[0].match(/\w/))
    {
        range.setStart(range.startContainer, range.startOffset - 1);
    }

    while (range.endOffset < range.endContainer.length && range.toString()[range.toString().length - 1].match(/\w/))
    {
        range.setEnd(range.endContainer, range.endOffset + 1);
    }
}
你的心境我的脸 2024-12-17 20:15:28

要恢复您的选择,请执行以下操作:

var range = selection.getRangeAt(0); // check for rangeCount in advance
var oldRange = document.createRange();
oldRange.setStart(range.startContainer, range.startOffset);
oldRange.setEnd(range.endContainer, range.endOffset);
...modify the selection and do whatever you need...
selection.removeAllRanges();
selection.addRange(oldRange);

To restore your selection, do this:

var range = selection.getRangeAt(0); // check for rangeCount in advance
var oldRange = document.createRange();
oldRange.setStart(range.startContainer, range.startOffset);
oldRange.setEnd(range.endContainer, range.endOffset);
...modify the selection and do whatever you need...
selection.removeAllRanges();
selection.addRange(oldRange);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文