-1

你好我的代码有问题..为什么它不起作用..?? 我的代码有问题吗?

function selectWord() {
        var select = window.getSelection();

        if (select.getBoundingClientRect) {        
            var rect = select.getBoundingClientRect ();
            x = rect.left;
            y = rect.top;
            w = rect.right - rect.left;
            h = rect.bottom - rect.top;

            alert (" Left: " + x + "\n Top: " + y + "\n Width: " + w + "\n Height: " + h);
        }
        else {
            alert ("Your browser does not support!");
        }
    }

谢谢你

4

2 回答 2

2

我的猜测是您在不支持它的浏览器中使用它。IE不支持getSelection,Fx 3.7应该是第一个支持getBoundingClientRect

Firefox 的 getBoundingClientRect 问题

于 2010-11-12T14:16:55.290 回答
1

getBoundingClientRect is a DOM Node method, the result of the getSelection method is not a DOM Node.

There might be a way using the anchorNode, anchorOffset, focusNode and focusOffset properties of the Selection that is returned.

If you use firefox + firebug, you can do console.log(select) and inspect the properties that you have access to.

于 2010-11-12T13:57:46.390 回答