简体   繁体   English

使用js将一个div的高度设置为与另一个div相同的高度

[英]Using js to set height of one div to the same height as another div

I'm trying to set the height of a div to the same height as another div using this 我正在尝试使用此将div的高度设置为与另一个div相同的高度

var aboutheight=document.getElementById('about').offsetHeight;
document.getElementById('sampledogs').setAttribute("style","height:aboutheight");

But the height stays the same! 但是高度保持不变! Can anyone help? 有人可以帮忙吗?

Thanks 谢谢

I like jQuery for this problem its simple and easy to read: 我喜欢jQuery,这个问题简单易读:

$('#sampledogs').height($('#about').height());

I find enough uses for jquery simplicity to include it in just about any project using javascript that I do. 我发现jquery简单性有足够的用途,可以将其包含在使用JavaScript的几乎所有项目中。 Makes working with the DOM much easier. 使使用DOM更容易。

JavaScript ain't inserting variables in strings. JavaScript不会在字符串中插入变量。 You need to concat it: 您需要进行合并:

document.getElementById('sampledogs').setAttribute("style", "height:" + aboutheight);
// or better
document.getElementById('sampledogs').style.height = aboutheight;

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

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