开发者

get the highest div from main div jquery

开发者 https://www.devze.com 2023-04-05 15:43 出处:网络
I\'ve seen this and this as well, but I just can\'t get them to work. I have a main div with many children with different ids and classes inside it.

I've seen this and this as well, but I just can't get them to work.

I have a main div with many children with different ids and classes inside it. I need to find a way the get the heights of a certain class and set the next divs' heights according to them. I know it's not working but it may help you understand what I would like to achieve:

var placesHeight = 0;
$('.places').each(function(){
    if(height > 35 ) {
    $(this).next('.nextdiv').css('height','(placesHeight+25)+"px"');
});

EDIT: Thanks for the quick help everyone. I tried all of it and realized all I need is this:

var placesHeight = $('.places').height();
$开发者_C百科('.nextdiv').css('height', placesHeight+25);


Here's your problem:

.css('height','(placesHeight+25)+"px"');

The value of placesHeight won't be parsed - it's going to try to set the value of the height as (placesHeight+25)+"px". It can be done more simply:

.css('height', placesHeight+25);

The "px" will be added automatically for you.


var placesHeight = 0;
$('.places').each(function(){
    if($(this).height() > 35 ) {
    $(this).next('.nextdiv').css('height',$(this).height()+25+"px");
});

something like that?


Try this instead

$(this).next('.nextdiv').height(placesHeight+25);


It would be helpful to see more of your markup - a jsfiddle would be best. Something like this might work though:

$('.places').each(function(){
    var $this = $(this),
        height = $(this).height();
    height > 35 && $this.next().height(height);
});


I hope this helps somehow,

<script type="text/javascript" src="jquery-1.6.3.min.js"></script>
<script>
function test(){
    $("#my_div").children('.check_me').each(function(){
        alert( $(this).outerHeight());
    });
}
</script>
<style>
#my_div div{
    display:block;
    margin:5px;
    border:1pt solid #ccc;
}
</style>
<button onclick='test()'>try me</button>

<div id='my_div'>
    <div id='one_line' class='check_me'>
        first
    </div>
    <div id='two_lines'>
        first<br>second
    </div>
    <div id='three lines' class='check_me'>
        first<br>second<br>third
    </div>
<div>
0

精彩评论

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