如何在window.getSelection中选择多个单词

发布于 2024-12-06 08:28:54 字数 816 浏览 0 评论 0原文

如何在 window.getSelection 中选择多个单词。

在此代码中,如果我选择 tweenen paragrap,它将锁定单词 paragraphs 并错过 ` Between 。我需要选择两个单词。

或者即使我选择3个词dd 2 spa,它也会选择3个词add 2 space。谢谢。

<script type="text/javascript">
    function Selected() {
        var sel;
        if ((window.getSelection) && (sel = window.getSelection()).modify) {
            sel = window.getSelection();
            if (!sel.isCollapsed) {
                sel.modify("move", "backward", "word");
                sel.modify("extend", "forward", "word");
            }
        }
    }
</script>
<p>put returns between paragraphs, for linebreak add 2 spaces at end</p>
<input type="button" onclick="Selected();" value="selected">

How to select multi words in window.getSelection.

In this code, if I choose tweenen paragrap, it will locked word paragraphs and missed `between. And I need select both 2 words.

Or even I choose 3 words dd 2 spa, it will select 3 words add 2 spaces. thanks.

<script type="text/javascript">
    function Selected() {
        var sel;
        if ((window.getSelection) && (sel = window.getSelection()).modify) {
            sel = window.getSelection();
            if (!sel.isCollapsed) {
                sel.modify("move", "backward", "word");
                sel.modify("extend", "forward", "word");
            }
        }
    }
</script>
<p>put returns between paragraphs, for linebreak add 2 spaces at end</p>
<input type="button" onclick="Selected();" value="selected">

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

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

发布评论

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

评论(1

请止步禁区 2024-12-13 08:28:54

如果我理解正确的话,您希望该按钮将所选内容对齐到整个单词。如果是这样,您可以这样做:

现场演示:http://jsfiddle.net/uCHVQ/1/< /a>

代码:(

function Selected() {
    var sel;
    if ((window.getSelection) && (sel = window.getSelection()).modify) {
        sel = window.getSelection();
        if (!sel.isCollapsed) {

            // Detect if selection is backwards
            var range = document.createRange();
            range.setStart(sel.anchorNode, sel.anchorOffset);
            range.setEnd(sel.focusNode, sel.focusOffset);
            var backwards = range.collapsed;
            range.detach();

            // modify() works on the focus of the selection
            var endNode = sel.focusNode, endOffset = sel.focusOffset;
            sel.collapse(sel.anchorNode, sel.anchorOffset);
            if (backwards) {
                sel.modify("move", "forward", "word");
                sel.extend(endNode, endOffset);
                sel.modify("extend", "backward", "word");

            } else {
                sel.modify("move", "backward", "word");
                sel.extend(endNode, endOffset);
                sel.modify("extend", "forward", "word");
            }
        }
    }
}

顺便问一下,该代码最初是来自我发布的内容吗?它看起来很熟悉,但我找不到它)。

If I understand you correctly, you want the button to snap the selection to whole words. If so, here's how you can do it:

Live demo: http://jsfiddle.net/uCHVQ/1/

Code:

function Selected() {
    var sel;
    if ((window.getSelection) && (sel = window.getSelection()).modify) {
        sel = window.getSelection();
        if (!sel.isCollapsed) {

            // Detect if selection is backwards
            var range = document.createRange();
            range.setStart(sel.anchorNode, sel.anchorOffset);
            range.setEnd(sel.focusNode, sel.focusOffset);
            var backwards = range.collapsed;
            range.detach();

            // modify() works on the focus of the selection
            var endNode = sel.focusNode, endOffset = sel.focusOffset;
            sel.collapse(sel.anchorNode, sel.anchorOffset);
            if (backwards) {
                sel.modify("move", "forward", "word");
                sel.extend(endNode, endOffset);
                sel.modify("extend", "backward", "word");

            } else {
                sel.modify("move", "backward", "word");
                sel.extend(endNode, endOffset);
                sel.modify("extend", "forward", "word");
            }
        }
    }
}

(By the way, is that code originally from something I've posted? It looks familiar but I can't find it).

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