简体   繁体   中英

How do you get div height in pure javascript?

什么是纯 javascript 中下面代码的等价物?

 $('.div1').height($('.div2').height());

这将是以下内容:

document.querySelector(".div1").style.height = window.getComputedStyle(document.querySelector(".div2")).height;

Using document.getElementsByClassName

var height = document.getElementsByClassName("div1")[0].style.height;
document.getElementsByClassName("div2")[0].style.height = height;

Loop through all the elements with the class name, setting them all to the desired height. Like this:

var div1s = document.getElementsByClassName(".div1");
var height = document.getElementsByClassName(".div2")[0].height;
for (int i = 0; i < div1s.length; i++) {
     div1s[i].height = height;
}

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