【问题标题】:position in javascriptjavascript中的位置
【发布时间】:2011-05-09 02:05:14
【问题描述】:

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

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 个隐含的全局变量 (x, y, w, h),更正一下。另外,为什么在函数名称和调用运算符之间放置空格?使用foo() 而不是foo ()

标签: javascript mozilla selectedtext


【解决方案1】:

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

getBoundingClientRect problem with Firefox

【讨论】:

    【解决方案2】:

    getBoundingClientRect 是一个 DOM Node 方法,getSelection 方法的结果不是一个 DOM Node。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2011-11-12
      • 2011-06-15
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      相关资源
      最近更新 更多