简体   繁体   English

你如何在纯 javascript 中获得 div 高度?

[英]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使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从纯JavaScript获取div的最长高度? - How to get the longest height of the div from pure javascript? 在javascript中点击“提交”按钮后,如何获取表格div +错误消息的高度? - How do you get the height of a form div + error messages after hitting the submit button in javascript? 如何在纯JavaScript中获得像素的高度? - How to get the height in pixels in pure javascript? 使用纯JavaScript获取PHP生成的div的高度 - Get height of PHP-generated div with pure javascript 如何使用纯 JavaScript 根据宽度调整 div 高度 - How resize a div height based on width with pure JavaScript 如何使用纯 JavaScript 获取 (display:none) 元素高度? - how to get (display:none)element height using pure JavaScript? 如何在JavaScript中获取单击的div元素的ID? - How do you get an ID of a clicked div element in JavaScript? 您如何定位此父级,从一个孩子开始,使用仅带有纯JavaScript的类,仅使另一个子div显示? - How do you target THIS Parent, from a child to make another child div show using nothing but classes with pure javascript? 如何在 javascript 中的纯 function 中更新 object? - How do you update an object in a pure function in javascript? 您如何将此代码转换为纯功能 javascript? - How do you transform this code into pure functional javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM