Get width of specific div and use as another div's height?

送分小仙女□ 提交于 2020-01-17 01:40:10

问题


I have two div's. #divA & #divB.

#divA has a width of 100% and I want to get the px value of the div and apply it to #divB as it's height CSS. I've tried using .offsetWidth but to no avail. Can anybody help me?


回答1:


use .css('width') to keep units intact (ex: 400px), use .width() to get just the numerical value (ex: 400) for use in mathematical computations.

for what you're doing:

var divAWidth = $('#divA').css('width');
$('#divB').css('height', divAWidth);

if you're getting mathematical:

var divAWidthNumber = $('#divA').width();
var halfThatWidth = divAWidthNumber / 2;



回答2:


If you don't want to use jQuery:

var getwidth = document.getElementById("div1").style.width;
document.getElementById("div").style.height = getWidth;

With jQuery

var getwidth = $("#div1").width();

EDIT

$("#div").height(getwidth);


来源:https://stackoverflow.com/questions/21057702/get-width-of-specific-div-and-use-as-another-divs-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!