浏览器中的全字选择

时间:2010-08-18 20:14:23

标签: javascript cross-browser selection

我知道IE有一个全字选择功能,人们经常想要关闭它。

我想要相反。

我想自动扩展选择以在所有浏览器中完成单词。实际上,目前我并不关心IE(但我最终会这样)。

任何人都知道这方面的解决方案吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以通过将选择的修改方法与mousemove事件结合使用,在WebKit(and apparently Firefox 4)中执行某些操作。尽管如此,要想做到这一点可能会非常棘手:你需要考虑何时适合扩大选择范围。

以下是使用modify方法的一个非常简单的示例:

<p>A paragraph with some nice text in it</p>

<script type="text/javascript">
    function expandSelection() {
        var sel;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.modify) {
                sel.modify("extend", "forward", "word");
            }
        }
    }
</script>

<input type="button" onclick="expandSelection();" value="expand">