的坐标使用JS
我想获取 a 的坐标,但我不知道该怎么做。我尝试使用 style.scrollTop、style.top 但我无法检索任何内容...在我的函数 posX = undefined 和 posXX == nada 中,
我将不胜感激您的帮助。
代码:
<html>
<title>
YOP
</title>
<head>
<style TYPE="text/css">
<!--
div.background{
width:450px;
height:450px;
background:url(plasmid.jpg) no-repeat;
border:2px solid black;
}
div.transbox{
top:0;
left:0;
width:150px;
height:150px;
margin:5px 5px;
background-color:#ff0000;
border:1px solid black;
/* for IE */
filter:alpha(opacity=60);
/* CSS3 standard */
opacity:0.6;
cursor: move;
}
-->
</style>
<script type="text/javascript" >
function MoveIt(){
obj = document.getElementById("arect");
posX = obj.style.scrollTop;
posXX = obj.style.top;
alert("x:"+posX + " xx:"+posXX);
}
</script>
</head>
<body bgcolor="white" >
<div class="background" id="MyDiv2">
<div id="arect" name="arect" class="transbox" onmousedown="MoveIt();" >
</div>
</div>
</body>
</html>
I would like to get the coordinates of a , but I cant figure how to do that. I tried using style.scrollTop, style.top but I cannot retrieve anything... In my function posX = undefined and posXX == nada
I would appreciate some help from you.
Code:
<html>
<title>
YOP
</title>
<head>
<style TYPE="text/css">
<!--
div.background{
width:450px;
height:450px;
background:url(plasmid.jpg) no-repeat;
border:2px solid black;
}
div.transbox{
top:0;
left:0;
width:150px;
height:150px;
margin:5px 5px;
background-color:#ff0000;
border:1px solid black;
/* for IE */
filter:alpha(opacity=60);
/* CSS3 standard */
opacity:0.6;
cursor: move;
}
-->
</style>
<script type="text/javascript" >
function MoveIt(){
obj = document.getElementById("arect");
posX = obj.style.scrollTop;
posXX = obj.style.top;
alert("x:"+posX + " xx:"+posXX);
}
</script>
</head>
<body bgcolor="white" >
<div class="background" id="MyDiv2">
<div id="arect" name="arect" class="transbox" onmousedown="MoveIt();" >
</div>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个棘手的问题,不同的浏览器实现它的方式非常不同,您可能必须考虑滚动和其他因素。我建议使用库(例如 jQuery),他们已经为我们解决了 x 浏览器问题。
This is a tricky one, different browsers implement it very differently, you may have to take scrolling and other factors into consideration. I would reccomend using a library (such as jQuery), they have solved the x-browser issues for us already.