1. 程式人生 > >關於DIV css樣式寬度設成百分比怎麼在瀏覽器中獲得它的畫素

關於DIV css樣式寬度設成百分比怎麼在瀏覽器中獲得它的畫素

html

<div class="btnNone">
    none情況下獲取Container寬度
</div>
<div class="btnBlock">
    block情況下獲取Container寬度
</div>

<div class="container none">
    <div class="item">
    </div>
</div>

css

    .container{
        width: 100%;
        height: 100%;
        background
: #FFFF80
; }
.block{ display: block; } .none{ display: none; } .item{ height: 50%; width: 50%; background: #80FF80; } .btnNone,.btnBlock{ width: 250px; height: 40px; font-size: 16px; background: #FF8080; color
: #ffffff
; font-weight: bolder; text-align: center; line-height: 40px; margin: 20px; position: fixed; z-index: 99; }
.btnBlock{ top: 100px; } .btnNone{ top:200px; }

js

(function ($) {


        $('.btnBlock').click(function
(){ $('.container').removeClass('block').removeClass('none').addClass('block'); alert($('.item').css('height')); })
$('.btnNone').click(function(){ $('.container').removeClass('none').removeClass('block').addClass('none'); alert($('.item').css('height')); }) }
)
(jQuery)

初始狀態
這裡寫圖片描述
none情況下獲取Container寬度
這裡寫圖片描述
block情況下獲取Container寬度
這裡寫圖片描述