2010-11-17 70 views
1

我對JavaScript很陌生(今天開始搞亂了)。(不能)在javascript中獲取元素樣式的一部分

我試圖改變一個名爲'bar'的元素(一個div)的高度。 酒吧將成爲圖表的一部分。

我沒有任何問題掛鉤到一個函數,它改變了酒吧的高度。所有的工作都很好,除了酒吧在向下的方向上生長,而不是像我想要的那樣朝上。

我的解決方案是增加「高度」屬性,然後減少「頂部」屬性 的高度增加量。

所以我想要做的事,如:

bar.style.height = newHeight; 
bar.style.top = bar.style.top - newHeight; 

,並應增加向上的高度。 的問題是,我不知道怎麼去bar.style.top

在Firebug的當前值,如果我看的HTML標籤的樣式,而酒吧的

top: 122px; 

但是在bar的監視列表中,我所看到的全是 bar.style.top的值「」(空字符串)。我想我的問題是 - 爲什麼這些值不匹配,我怎樣才能達到我想要的價值?

雖然我問過這樣的事一兩件事:

我對WebKit的一些CSS,看起來像:

-webkit-animation-timing-function: ease-in-out; 

例如。我注意到在酒吧的監視列表中,沒有bar.style.-webkit-animation-timeing-function。我如何獲得並更改這樣的值?

我通常使用asp.net,我希望有類似

bar.Style["property"] = "value"; 

我錯過了東西在這裏的東西嗎?

+0

您是否已經設置了'position'屬性? – 2010-11-17 22:29:05

+0

@Evan,它被設置爲絕對 – Erix 2010-11-17 22:33:27

回答

3
  1. 請使用document.getElementById('bar'),而不是僅僅bar,因爲bar可能意味着很多不同的事情。
  2. style屬性允許您覆蓋任何樣式,但只給出由<div style="...">屬性設置的樣式。有關如何獲取當前樣式,請參閱http://www.quirksmode.org/dom/getstyles.html
+0

1。在代碼中,我正在這樣做。只是想節省空間。酒吧是一個元素。 – Erix 2010-11-17 22:34:12

+0

2.謝謝我會檢查出來 – Erix 2010-11-17 22:34:33

1

既然你去一個柱狀圖,我會建議造型有點不同使用的「底部」,而不是「頂」。然後,你只需要改變高度。類似以下內容:

<div style="position: relative; width: 500px; height: 300px; border: 1px solid black;"> 
    <div style="position: absolute; bottom: 10px; left: 10px; width: 20px; height: 40px; background-color: red;"></div> 
    <div style="position: absolute; bottom: 10px; left: 40px; width: 20px; height: 60px; background-color: blue;"></div> 
    <div style="position: absolute; bottom: 10px; left: 70px; width: 20px; height: 80px; background-color: green;"></div> 
</div>