2012-08-06 28 views
5

Bu kod bir eleman yüksekliğini ayarlamalıdır; ancak hiçbir stil eklenmez. Belli bir şey eksik miyim?Javascript kullanarak yüksekliği ayarlayın

function setGround() { 
    document.getElementById('content').style.height = '40px'; 
} 

document.onload = setGround; 

HTML oldukça basit:

<!DOCTYPE html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Template</title> 
    <link rel="stylesheet" type="text/css" href="css/default.css"/> 
    <link rel="stylesheet" type="text/css" href="css/orange.css"/> 
    <script type="text/javascript" src="javascript/detect-css.js"></script> 
</head> 

<body> 
    <header> 
     <div id="logo"></div> 
    </header> 
    <section id="sidebar"><p>sf </p> 
    </section> 

    <section id="content"><p>sf </p> 
    </section> 

</body> 
</html> 

Yardımlarınız için teşekkür ederiz!

+1

ait kullanmalıdır. Sonra sadece setGround() 'yapabilirsiniz. –

cevap

4

Yerine document.onloadwindow.onload kullanın. Nerede olduğun

<section id="content" style='border:1px solid fuchsia;' > 
<p>sf </p> 
</section>  
+0

çalışıyor! teşekkür ederim! Farkın ne olduğunu açıklar mısınız? – cmplieger

+0

Bu tartışma hakkında konuşuyor http://stackoverflow.com/questions/588040/window-onload-vs-document-onload – MattK311

2

bunu kullanabilirsiniz gör Javascript kodu yerleştirildi mi? Detect-css.js içinde mi?

Bu çalışır:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Template</title> 
<link rel="stylesheet" type="text/css" href="css/default.css"/> 
<link rel="stylesheet" type="text/css" href="css/orange.css"/> 
<script language="javascript"> 
function resize(){ 
document.getElementById("content").style.height='100px'; 
} 
</script> 
</head> 

<body onload="resize()"> 
<header><div id="logo"></div></header> 
<section id="sidebar"><p>sf </p> 
</section> 

<section id="content" style="background-color:#CCC; display:block;"><p>sf </p> 
</section> 

</body> 
</html> 
0

:

function setGround() { 
    document.getElementById('content').style.height = '40px'; 
} 
document.onload = setGround; 

ancak değişiklikleri görmek isterseniz kullanımı bununla bölüm etiketi üzerinde sınır oluşturmalısınız:

http://jsfiddle.net/mowglisanu/r6NzE/

1

Sen

window.onLoad = setGround; 

yerine çok basit bir çözüm sadece `` etiketinden önce senaryoyu bastırmak için

document.onload = setGround; 
İlgili konular