获取没有滚动条宽度的Div宽度

时间:2022-11-19 13:43:24

I am having a issue here. There are two div's here "div1" and "div2". Here I want to adjust the "div1" width according to the "div2" width. My requirement is The width of the scrollBar should not be included for the "div1" i.e i should set the height of the "div1" excluding the width of the scrollBar in "div2". I want a jquery function to find the width and to reduce the width of "div1".. Sample requirement is shown below

我在这里有一个问题。这里有两个div“div1”和“div2”。这里我想根据“div2”宽度调整“div1”宽度。我的要求是“div1”不应该包含scrollBar的宽度,即我应该设置“div1”的高度,不包括“div2”中scrollBar的宽度。我想要一个jquery函数来查找宽度并减小“div1”的宽度。样本要求如下所示

html

<div class="div1"></div>
<div class="div2">HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello</div>

css

.div1
{
    width:100px;
    height:100px;
    border:1px solid black;
}
.div2
{
    width:100px;
    height:300px;
    border:1px solid black;
    overflow:auto;
    word-break:break-all;
}

获取没有滚动条宽度的Div宽度

The Fiddle Demo is here

小提琴演示就在这里

Fiddle

1 个解决方案

#1


6  

See this: http://jsfiddle.net/d34Fk/5/

见:http://jsfiddle.net/d34Fk/5/

jQuery :

var width = $('.div2')[0]['clientWidth'];
$('.div1').css({'width':width+'px'});

#1


6  

See this: http://jsfiddle.net/d34Fk/5/

见:http://jsfiddle.net/d34Fk/5/

jQuery :

var width = $('.div2')[0]['clientWidth'];
$('.div1').css({'width':width+'px'});