2012-10-21 138 views
0

我正在嘗試製作旗幟。該橫幅用寬度和高度的百分比製成。當身體被裝載時,它應該在所有分辨率下看起來不錯。但是,我的代碼似乎不工作。如果任何人都可以看看它,將不勝感激。加載旗幟高度和寬度

<script> 
    function load() 
    { 

     document.write("Available Width: " + Math.round((screen.availWidth)/5)); 

     a=document.getElementById('banner'); 
     a.style.width=Math.round((screen.availWidth)); 
     a.style.height=Math.round((screen.availHeight)/5); 
    } 
    </script> 

回答

0

您需要指定樣式屬性的單位。

a.style.width = … + 'px'; 

此外,使用了a變量var語句,這樣可以防止a泄漏到全球範圍內(如果它不打算)。

更新:另請嘗試刪除document.write調用。

+0

我將它改爲\t a.style.width = Math.round((screen.availWidth))+'px'; \t \t但它似乎仍然不工作 –

+0

那麼,在這種情況下,你是否在DOM樹中存在'id =「banner」'元素時調用函數?另外嘗試刪除'document.write'行。 –

+0

也許有問題嗎? –