English 中文(简体)
getBoundingClientRect 在Firefox中出现问题。
原标题:getBoundingClientRect problem with Firefox

我正在尝试使用getBoundingClientRect()获取可编辑div中光标的y坐标。IE分支代码有效,但其他分支(例如Firefox 3.5,用于当前测试目的)无效。

以下代码中具有问题的行已在注释中用***标记。在代码的那一点上,selObj和selRange都具有值(在Firebug中确认),但我无法调用它们中的任何一个的getBoundingClientRect()(例如,gives e.g.selObj.getBoundingClientRect is not a function)。我已经阅读到getBoundingClientRect()现在在Firefox上支持Range对象,但我无法让它发挥作用。我猜我必须在错误的对象类型上调用它...?我应该调用它吗?

以下代码是包含相关javascript的完整测试案例的html文件。在IE中查看,我可以得到光标的y坐标值,但在Firefox中它会弹出。

<html>
<head>
<title>Test</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
#pageContainer {
    padding:50px;
}
.pageCommon {

    width: 100px; 
    height: 100px;
    background-color:#ffffD0;
    padding: 10px;
    border: 1px solid black;
    overflow: auto;
}


</style>
</head>
<body>
<div id="pageContainer">
    <div id="editor" class="pageCommon" contenteditable onclick="setPageNav();" onkeypress="setPageNav();">

    </div>
    <div>y: <span id="y"></span></div>

</div>
<script>
var y;

function setPageNav() {

    page = document.getElementById("editor"); 
    if (window.getSelection) {
            var selObj = window.getSelection();
            var selRange = selObj.getRangeAt(0);
            // *** Neither of these next two lines work, error is : selObj.getBoundingClientRect is not a function
            y = selObj.getBoundingClientRect().top;
            y = selRange.getBoundingClientRect().top;
    } else if (document.selection) {
            var range = document.selection.createRange();
            y = range.getBoundingClientRect().top;
    }
    document.getElementById("y").innerHTML = y;
}

</script>
</body>
</html>
问题回答

我已经阅读到,getBoundingClientRect()现在在Firefox上支持Range对象。

还没有。这是Mozilla 1.9.3的一个功能,您可以在Firefox 3.7中期待。

你们需要一帆风顺,因为它没有得到任何其他浏览者的支持。

我已经阅读到,getBoundingClientRect()现在在Firefox上支持范围对象。

该支持功能是Gecko 1.9.3 alpha新推出的,将在Firefox 3.6.x之后版本中包含。Firefox 3.5不支持该功能。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签