Javascript并没有改变我想要的对象的CSS

时间:2017-11-15 19:07:08

标签: javascript html css

我的代码必须更改元素循环的CSS,但每当我单击按钮(也是循环)时,它会更改代码中的第一项,其id为=" big" 。我希望它能改变它下面的元素。我的代码是:

HTML:

<button onclick="getbig()">
<img style="height:50;width:800;" src="imgsrc">
</button>
<div id="big" style="background-color:white;"></div>

&#34;大&#34;是id我想改变:

JS:

var big = 1;
 function getbig(){
     if(big == 1){
        big = 2;
        document.getElementById('big').style.cssText =  "height:300;width:800;";
     }
     else{
        big = 1;
        document.getElementById('big').style.cssText =  "max-height:0;max-width:0;";
     }
 }

1 个答案:

答案 0 :(得分:0)

您需要将单位px添加到以下值:

&#13;
&#13;
var big = 1;

function getbig() {
  if (big == 1) {
    big = 2;
    document.getElementById('big').style.cssText = "height:300px;width:800px;";
  } else {
    big = 1;
    document.getElementById('big').style.cssText = "max-height:0;max-width:0;";
  }
}
&#13;
#big {
  background: red;
}
&#13;
<button onclick="getbig()">
<img style="height:50px;width:80px;" src="https://lorempixel.com/800/800/">
</button>
<div id="big">
</div>
&#13;
&#13;
&#13;