jQuery的:基于另一个元素改变高度 [英] JQuery: Changing Height Based on Another Element

查看:222
本文介绍了jQuery的:基于另一个元素改变高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个DIV:一个DIV根据浏览器(响应式设计)动态更改大小,我希望另一个DIV根据第一个DIV的高度修改其高度.我认为最简单的方法是使用JQuery动态更改高度.

I have two DIVs: One DIV dynamically changes size based on the browser (responsive design) and I want the other DIV to modify its height based on the first DIV's height. I thought the easiest way to do this would be to use JQuery to dynamically change the height.

我尝试了以下操作:

$('section#div2').css({height:$(this).css('height').replace($('section#div1 div.div1_container').height()+'px')});

但是,该代码无法正常工作.各种其他版本也不起作用.

However, that code isn't working. Nor are various other versions of that working.

我的问题是:如何在不更改给定元素的所有属性的情况下替换单个CSS属性?

My question is: How can I replace a single CSS property without changing all of the properties for a given element?

这是动态第一个DIV编码的方式...

This is how the dynamic first DIV is coded...

首先是第一个DIV的容器:

First the first DIV's container:

position: relative;
padding-bottom: 50%;
padding-top: 30px; height: 0; overflow: hidden;
margin:0 0.7em 0 0.7em;

然后,该容器内的内部对象(可能不相关):

Then, the inner object within that container (may not be relevant):

position:absolute;
top:0;
left:0;
width:100%;
height:100%;

推荐答案

$('section#div2').css('height', $('section#div1').height()+'px');

这篇关于jQuery的:基于另一个元素改变高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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