本文介绍了设置div的宽度等于另一个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想给.second_div
设置first_div
的宽度,但是问题是第一个div没有css中给出的宽度值,因此我需要使用jQuery查找其宽度.
I want to give my .second_div
the width of first_div
, but the problem is that the first div does not have a width value given in css and I need to find his width using jQuery.
HTML:
<div class="first_div">First Div</div>
<div class="second_div">Second.width == First.width</div>
CSS:
.second_div,
.first_div {
border: 1px solid black;
padding: 2px 4px;
display:inline-block;
}
jQuery:
$(document).ready(function(){
$(".second_div").css({'width':($(".first_div").width()+'px')});
});
jsfiddle: https://jsfiddle.net/ekqecjb8/1/
jsfiddle: https://jsfiddle.net/ekqecjb8/1/
推荐答案
在您的小提琴中缺少jQuery库,您的代码中没有错误.只需在您的代码中包含jQuery库
Missing jQuery library in your fiddle, there is no error in your code . Just include jQuery library in your code
$(document).ready(function() {
$(".second_div").css({
'width': ($(".first_div").width() + 'px')
});
});
.second_div,
.first_div {
border: 1px solid black;
padding: 2px 4px;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="first_div">First Div</div>
<div class="second_div">Second.width == First.width</div>
这篇关于设置div的宽度等于另一个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!