简体   繁体   中英

get div width jquery

I need to show a div (called over) when I go over it with cursor. Over div has position absolute. I did it using jquery. It works good, but the only problem is that over div doesn't get parent div width. I do it using jquery, this is the code:

var count = 0;
$('.resultbox').each(function(){
    $('#main').append('<div class="content" id="t'+count+'"></div>');
    $('#t' + count).append('<div class="over"></div>');

    $('#t'+count).append('<span>some text here</span>');

    $('.asd', this).each(
            $('#t' + count).append('<span>and other text here</span>');
        });
    });
    //alert('');
    $('#t' + count + ' > .over').css('width', $('#t' + count).css('width'));
    $('#t' + count + ' > .over').css('height', $('#t' + count).css('height'));
    count++;
});

$('.content').mouseenter(function(){
    $('.over', this).show();
}); 

$('.content').mouseleave(function(){
    $('.over', this).hide();
});

and css is:

#main{
width: 100%;
position: absolute;
top: 0px;
height: 100%;
background-color: #FFF;
z-index: 999;
text-align: left;   
}

.content{
width: auto;
display: inline-block;
margin: 20px;
padding: 5px;
border: 1px solid #000;
clear: both;
}

.content span{
display: inline-block;
}

.over{
position: absolute;
background: url(share.png) #FFF no-repeat center center;
opacity: 0.6;
z-index: 999;
display: none;
}

.over:hover{
cursor: pointer;
}

As you can see I put also an alert. If I comment it over div doesn't get his parent width, while if i don't comment it, over div get the right parent width. Why!?

Thanks, Mattia

我闻起来好像是在尝试找到宽度时未加载HTML中的父元素。当发出警报时,父元素已加载。确保确定加载时尝试查找父元素的宽度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM