简体   繁体   中英

Set element height by the use of javascript variable

I am looking to obtain the height of a visitors viewport and then use this value to change an element's height. I have so far used Javascript to store the viewport height into a variable. I then attempted to select the elements and change the "height" style to the visitors viewport height by using the Javascript variable as the input.

The error I get is:


var h = Math.max(document.documentElement.clientHeight || 0);
h = h + "px";
document.getElementsByTagName("form", "label").style.height = h;

To throw a (unrequested) CSS solution into the mix, this is very easy to achieve with plain CSS:
http://codepen.io/panchroma/pen/bgwpzz

CSS

form, label{height:100vh;}

As comments said, it takes only 1 string. To change the height of the label, you need to wrap it into a div, or p... Then:

 var h = Math.max(document.documentElement.clientHeight || 0);
    h = h + "px";

    var forms = document.getElementsByTagName("form");
    for(var i=0;i<form.length;i++){
    forms[i].style.height = h;
    }
    var divs = document.getElementsByTagName("div");
    for(var j=0;j<divs.length;j++){
    divs[j].style.height = h;
    }
var h = Math.max(document.documentElement.clientHeight || 0);
h = h + "px";

//here you have a collection of labels
var lbl = document.getElementsByTagName("label");
  for(var i=0;i<lbl.length;i++){
  lbl[i].style.height = h;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM