开发者

give element same width as previous sibling

Using jQuery I want to give a specific element the same width (or min-width in thi开发者_运维问答s case) as the element before (sibling). In my case there is an unordered list (UL), which I want to give the same width as the div before. For example:

<div style="width:300px;"></div>
<ul class="list"><li>item1</li><li>item2</li></ul>
<div style="width:200px;"></div>
<ul class="list"><li>item1</li><li>item2</li></ul>

So the width of the UL should depend on the previous sibling and not on any other element.


Does

$('ul').each(function()
{
    var width = $(this).prev().width();
    $(this).width(width);
});

do it? If you want to do it by getting/setting CSS instead:

$('ul').each(function()
{
    var width = $(this).prev().css('width');
    $(this).css('width', width);
});


$('.list').each(function() {
    var width = $(this).prev().width();
    $(this).width(width);
}):
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜