获取特定div的宽度并用作另一个div的高度? [英] Get width of specific div and use as another div's height?

查看:180
本文介绍了获取特定div的宽度并用作另一个div的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个div. #divA& #divB.

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

#divA的宽度为100%,我想获取div的px值,并将其应用于#divB,因为它是CSS的高度.我尝试使用.offsetWidth,但无济于事.有人可以帮我吗?

#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?

推荐答案

使用.css('width')保持单位完整(例如:400px),使用.width()获得数字值(例如:400 )用于数学计算.

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

您正在做什么:

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

如果您要数学:

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

这篇关于获取特定div的宽度并用作另一个div的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆