我希望在调用时从带有javascript函数的div中减去50px。为什么这不起作用?
<!DOCTYPE html>
<html>
<head>
<script>
function resizeDiv(id)
{
obj.style.height = ( parseFloat( obj.style.height ) - 50 ) + 'px';
}
</script>
<style type="text/css">
#divId{
background:blue;
width:300px;
height:100px;
</style>
</head>
<body>
<button onclick="resizeDiv('divId')">Try it</button>
<div id="divId"></div>
</body>
</html>
答案 0 :(得分:0)
您没有为高度定义obj或样式属性,并且错过了css中的括号:
<!DOCTYPE html>
<html>
<head>
<script>
function resizeDiv(id){
var obj=document.getElementById(id);
if(obj){
obj.style.height= (obj.offsetHeight- 50)+ 'px';
}
}
</script>
<style type= "text/css">
#divId{
background:blue;
width:300px;
height:100px;
}
</style>
</head>
<body>
<button onclick="resizeDiv('divId')">Try it</button>
<div id="divId"></div>
</body>
</html>