Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的位置_Javascript_Mozilla_Selectedtext - Fatal编程技术网

javascript中的位置

javascript中的位置,javascript,mozilla,selectedtext,Javascript,Mozilla,Selectedtext,你好,我的代码有问题。。 为什么它不起作用。。?? 我的代码有毛病吗 function selectWord() { var select = window.getSelection(); if (select.getBoundingClientRect) { var rect = select.getBoundingClientRect (); x = rect.left;

你好,我的代码有问题。。 为什么它不起作用。。?? 我的代码有毛病吗

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

谢谢

getBoundingClientRect是一个DOM节点方法,getSelection方法的结果不是DOM节点

可能有一种方法可以使用返回的选择的anchorNode、anchorOffset、focusNode和focusOffset属性


如果您使用firefox+firebug,您可以执行console.log(选择)并检查您有权访问的属性。

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

什么叫“它不工作”?你期望得到什么样的输出?你得到了4个隐含的全局变量(x,y,w,h),对吗。另外,为什么要在函数名和调用运算符之间加空格?使用
foo()
而不是
foo()