爲什麼我無法讀取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
和''
錯過'