开发者

position in javascript

开发者 https://www.devze.com 2023-01-24 14:20 出处:网络
hello i have a problem with my code.. why it doesn\'t work..?? is there fault with my code? function 开发者_如何学编程selectWord() {

hello i have a problem with my code.. why it doesn't work..?? is there fault with my code?

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!");
        }
    }

thank you


My guess is you are using this in a browser that does not support it. IE does not support getSelection and Fx 3.7 should be the first one to support the getBoundingClientRect

getBoundingClientRect problem with Firefox


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号