2017-01-16 104 views
1

我无法使用此代码更改Internet Explorer 7中的div高度,该代码适用于其他浏览器。在Internet Explorer 7中更改div高

document.getElementById('my_div').setAttribute("style","height:1000px !important"); 
    var clientHeight = document.getElementById('my_div').clientHeight; 
+0

不要这样做。 相反,通过调用attachEvent/addEventListener.http://www.quirksmode.org/js/events_advanced.html –

+0

@MirzaObaid添加一个事件处理程序这与事件无关 – skyline3000

回答

1

您需要通过

document.getElementById('my_div').style.height = '1000px'; 

这是一个完整的运行例子来改变这一行

document.getElementById('my_div').setAttribute("style","height:1000px !important"); 

function changeDivHeight(){ 
 
    var oldHeight = document.getElementById('my_div').clientHeight; 
 
\t console.log('old Height:', oldHeight); 
 
\t 
 
\t var val = document.getElementById('newHeight').value; 
 
\t document.getElementById('my_div').style.height = val + 'px'; 
 
\t 
 
    var newHeight = document.getElementById('my_div').clientHeight; 
 
\t console.log('new Height:', newHeight); 
 
}
#my_div{ 
 
\t background-color: #d1d1f1; 
 
\t width: 400px; 
 
\t height: 20px; 
 
}
New height value 
 
<input id="newHeight" type="text" placeholder="New div height" /> 
 
<button onclick="changeDivHeight()">Change height</button> 
 
<div id="my_div"></div>

+0

在Internet Explorer上不起作用7 –