您需要通过
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>
不要这样做。 相反,通过调用attachEvent/addEventListener.http://www.quirksmode.org/js/events_advanced.html –
@MirzaObaid添加一个事件处理程序这与事件无关 – skyline3000