我试图使用getBoundingClientRect()在可内容的div中获取游标的y坐标。代码的IE分支工作,但另一个分支(即Firefox 3.5,用于我当前的测试目的)不能工作。
下面的代码在注释中用*标记了有问题的行。在代码的那个时候,selObj和selRange都有一个值(在Firebug中确认),但是我不能对它们调用getBoundingClientRect() (例如,selObj.getBoundingClientRect不是函数)。我读过getBoundingClientRect()现在在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>发布于 2010-02-11 17:07:48
我看过getBoundingClientRect()现在在Range对象上的Firefox上得到了支持
还没有,这是Firefox 3.7中的Mozilla 1.9.3特性。
无论如何,您都需要回退,因为其他浏览器都不支持它。
发布于 2010-02-11 17:07:27
我看过getBoundingClientRect()现在在Range对象上的Firefox上得到了支持
对此的支持在Gecko 1.9.3 alpha中是新的,它将包含在3.6.x之后的火狐版本中。Firefox 3.5不支持它。
https://stackoverflow.com/questions/2245013
复制相似问题