为什么我无法读取object.style.left
& object.style.top
值?Javascript:为什么我无法读取'object.style.left'和'object.style.top'值?
我想动态地移动一个按钮。
我可以使用JavaScript通过这样的style.left
& style.top
值重置:
document.getElementById(theButton_ID).style.left = "128px";
document.getElementById(theButton_ID).style.top = "64px";
但我的问题是,我需要动态重置“style.left” &“style.top的价值观,使用需要在网页加载时动态确定的缩放因子。
要我想这个新手的方式意味着,我需要:
•获取style.left
& style.top
值
•比例因子乘以他们
•分配这些修改后的值,以style.left
& style.top
值。
问题是我无法获得第一个值的style.left
& style.top
所以我不能修改它们,更不用说写回它们。
很明显,我都做错了&我不理解我应该理解的东西。
那么我做错了什么?
我错过了什么?
,最重要的是,我怎么得到style.left
& style.top
这样的价值,我可以用它们来动态地修改按钮的位置?
感谢大家对我们的所有帮助&的建议。
下面是实际的HTML/CSS/Javascript代码...
<html>
<head>
<title>Button Dynamic Move Test</title>
<style type="text/css">
#MoveButton
{
position : absolute;
left : 8px;
top : 16px;
}
</style>
<script>
// var X_Index_Move_Factor = 1.25;
// var Y_Index_Move_Factor = 1.50;
function Move_A_Button (theButton_ID)
{
alert ( "We're in 'Move_A_Button'"
+ "\n"
+ " The Button Id = "
+ theButton_ID
);
var the_Old_Button_X_Offset = document.getElementById(theButtonId).style.left;
var the_Old_Button_Y_Offset = document.getElementById(theButtonId).style.top;
alert ( "the_Old_Button_X_Offset = "
+ "\n"
+ the_Old_Button_X_Offset
+ "the_Old_Button_Y_Offset = "
+ "\n"
+ the_Old_Button_Y_Offset
);
}
</script>
</head>
<body>
<button type = "button"
id = "MoveButton"
onclick = "Move_A_Button ('MoveButton')">
About Us
</button>
</body>
</html>
怪异的HTML代码格式化。 ! – Raptor 2013-05-08 02:44:26
theButtonId = theButton_ID – Lennart 2013-05-08 02:45:16
和''
错过'