2011-10-13 57 views
1
<script type="text/javascript"> 

     var step = 4; 

     function expandPanel() 

     { 

      var panel = document.getElementById('panel'); 

      if (panel.clientHeight < (panel.originalHeight-step)) 

      { 

       //clientWidth 

       var h = panel.clientHeight + step; 

       panel.style.height = h+"px"; 

       setTimeout("expandPanel()", 100); 

      } 

      else 

      { 

       panel.style.height = ""; 

       var panelTitle = document.getElementById('panelTitle'); 

       panelTitle.firstChild.nodeValue = 'Collapse'; 



      } 

     } 



     function collapsePanel() 

     { 

      var panel = document.getElementById('panel'); 



      if (panel.clientHeight >= step) 

      { 

       var h = panel.clientHeight - step; 

       panel.style.height = h+"px"; 

       setTimeout("collapsePanel()", 100); 

      } 

      else 

      { 

       panel.style.display = 'none'; 

       var panelTitle = document.getElementById('panelTitle'); 

       panelTitle.firstChild.nodeValue = 'Expand'; 

      } 





     } 



     function changePanel() 

     { 

      var panel = document.getElementById('panel'); 

      if (!panel.style.height || 

       (panel.style.display == 'none')) 

      { 

       if (panel.style.display == 'none') 

       { 

        panel.style.display = ''; 

        expandPanel(); 

       } 

       else 

       { 

        panel.originalHeight = panel.clientHeight; 

        collapsePanel(); 

       } 

      } 

     } 

    </script> 

有一個空字符串分配給heightdisplay CSS屬性(通過CSSOM)。 在這種情況下是什麼意思?這是空字符串是什麼意思?

+0

爲什麼你有這麼多'newlines'分離每一行代碼? –

回答

2

它所做的只是抹去CSS屬性。如果樣式屬性看起來像在此之前:

<div style="height: 100px"></div> 

現在看起來就像這樣:

<div style=""></div> 
+0

那麼,會使用默認值?? – DrStrangeLove

+0

您正在混淆屬性和屬性。該屬性可能存在也可能不存在,並且可能具有或可能不具有與該屬性相同的值。 – RobG

+1

將使用默認值或通過CSS繼承的任何屬性。這取決於是否有任何CSS規則適用於元素。 @RobG,風格屬性直接對應於firefox中style屬性本身的結果,看起來像IE不會更新樣式標記。 – SoWeLie

1

設置element.style屬性爲「」(空字符串),讓他們通過繼承或缺省值。

例如,設置element.style.display空字符串,它表明,以前隱藏着display = 'none';元素的首選方式,這樣你不必知道原來的財產是什麼(它可能沒有被定爲所有)。

請注意,在某些瀏覽器中,更改DOM屬性將修改相關的HTML屬性,但在其他瀏覽器中則不會。此外,如何設置屬性是實現相關的。所以不要依賴那個行爲,只處理屬性。

嘗試在幾個瀏覽器中執行以下操作:

<div id="d0">div</div> 
<button onclick=" 
    var el = document.getElementById('d0'); 
    el.style.backgroundColor = 'red'; 
    el.style.border = '1px solid blue'; 
    alert(el.getAttribute('style')); 
">Do stuff</button> 

火狐5:背景色:紅色;邊框:1px純藍色;

IE 8: [對象]

鉻14:背景色:紅色; border-top:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid;邊框頂部顏色:藍色;邊界 - 右邊顏色:藍色; border-bottom-color:blue; border-left-color:blue;

Opera 11: background-color:#ff0000; border-top-color:#0000ff; border-left-color:#0000ff; border-right-color:#0000ff; border-bottom-color:#0000ff; border-top:1px; border-left-width:1px; border-right-width:1px; border-bottom-width:1px; border-top-style:solid; border-left-style:solid; border-right-style:solid;下邊框風格:固體

0

這個例子應該有所幫助:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <title></title> 
     <style> 
      div { 
       height: 45px; 
       width: 45px; 
       border: 1px solid #000; 
       background-color: #ccc 
      } 
     </style> 
     <script> 
      window.addEventListener("DOMContentLoaded", function() { 
       var div = document.getElementsByTagName("div")[0]; 
       alert("That's the default size. Let's change it."); 
       div.style.height = "200px"; 
       div.style.width = "200px"; 
       alert("Let's reset the size back to default."); 
       div.style.height = ""; 
       div.style.width = ""; 
      }, false); 
     </script> 
    </head> 
    <body> 
     <div></div> 
    </body> 
</html>