JavaScript用三元运算符改变高度

时间:2017-06-09 10:58:23

标签: javascript html css

我试图在点击时更改宽度和高度,如果宽度已经是100%我想将其更改为1400px,但以某种方式跟随不起作用:

document.getElementById('btnFullscreen').addEventListener('click', function() {
    var wrapper = document.getElementById('wrapper');
    var container = document.getElementById('container');
    var imageView = document.getElementById('imageView');
    var canvasBody = document.getElementById('canvas-body');


    var items_arr = [canvasBody, videoPreview, wrapper, container, leftImageBox, imageView];

    for (i = 0; i < items_arr.length; ++i) {
        items_arr[i].style.width = items_arr[i].style.width != "100%;" ? "100%;" : "1400px;";
        items_arr[i].style.height = items_arr[i].style.height != "100%;" ? "100%;" : "1400px;";
        items_arr[i].style.minWidth = items_arr[i].style.minWidth != "100%;" ? "100%;" : "1400px;";
        items_arr[i].style.minHeight = items_arr[i].style.minHeight != "100%;" ? "100%;" : "1400px;";
    }
});

1 个答案:

答案 0 :(得分:0)

删除;,其中dom值为100%;。此代码videoPreviewleftImageBox都未定义

document.getElementById('btnFullscreen').addEventListener('click', function() {
  var wrapper = document.getElementById('wrapper');
  var container = document.getElementById('container');
  var imageView = document.getElementById('imageView');
  var canvasBody = document.getElementById('canvas-body');
  var items_arr = [canvasBody, videoPreview, wrapper, container, leftImageBox, imageView];

  for (i = 0; i < items_arr.length; ++i) {
    items_arr[i].style.width = items_arr[i].style.width != "100%;" ? "100%" : "1400px";
    items_arr[i].style.height = items_arr[i].style.height != "100%;" ? "100%" : "1400px";
    items_arr[i].style.minWidth = items_arr[i].style.minWidth != "100%;" ? "100%" : "1400px";
    items_arr[i].style.minHeight = items_arr[i].style.minHeight != "100%" ? "100%" : "1400px";
  }
});