2006-03
27
DOM对象event仅在事件发生的期间内存在。因此下面这种写法是错误的:
<a href="javascript:alert(window.event.x)">
而应当写成
<a href="#" onclick="alert(window.event.x); return false">
2006-03
27
通常,如果对HTML元素指定了 position: absolute,则可以通过 left、top、pixelLeft等 属性获取该元素的位置和大小。但是不指定 position:absolute 的情况下则不能使用该方法。
幸运的是如果我们想知道的是文本框的位置,则可以使用 textRange 对象来获取。
如果我们有
<input type="text" id="inputbox">
则可以通过以下代码获取其位置:
var inputbox = document.getElementById('inputbox');
var tr = inputbox.createTextRange();
top = tr.boundingTop;
left = tr.boundingLeft;
height = tr.boundingHeight;
