<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.d
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/l...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta charset="utf-8">
<style>
#div1{
width:300px;
height:300px;
border:1px solid red;
background:blue;
}
</style>
<script>
function setstyle(name,value){
var odiv=document.getElementById('div1')
odiv.style['name']=value;
}
</script>
</head>
<body>
<input type="button" value="变色" onclick="setstyle("background","red")" />
<input type="button" value="变宽" onclick="setstyle("width","350px")" />
<div id="div1">
</div>
</body>
</html>
为什么点击变色和变宽按钮:不变宽变色呢? 展开
<html>
<head>
<title> New Document </title>
<meta charset="utf-8">
<style>
#div1{
width:300px;
height:300px;
border:1px solid red;
background:blue;
}
</style>
<script>
function setstyle(name,value){
var odiv=document.getElementById('div1')
odiv.style['name']=value;
}
</script>
</head>
<body>
<input type="button" value="变色" onclick="setstyle("background","red")" />
<input type="button" value="变宽" onclick="setstyle("width","350px")" />
<div id="div1">
</div>
</body>
</html>
为什么点击变色和变宽按钮:不变宽变色呢? 展开
2个回答
展开全部
改两个地方:
function setstyle(name,value){
var odiv=document.getElementById('div1');
odiv.style[name]=value; // 去掉引号
}
<!-- 函数调用部分的双引号改为单引号 -->
<input type="button" value="变色" onclick="setstyle('background','red')" />
<input type="button" value="变宽" onclick="setstyle('width','350px')" />
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询