Assigned width in percentage but want to get it in pixels

前端 未结 11 1155
北荒
北荒 2020-12-15 15:07



        
11条回答
  •  伪装坚强ぢ
    2020-12-15 15:32

    Just an idea...don't hate. I know it doesn't cover every eventuality but something like this could check each of the items parents for the desired css rules (display:none; in this case). I got the idea from here.

    The only problem is when a site gets more complicated...it becomes slow.

    But it's a jumping off point.

    //showing as 100%
    alert($('#test2').width());
    //use window.load to ensure .width() isnt doing anything funny
    $(window).load(function(){
    //checks to see if any of its parents have display:none; we can have multiple checks here if required
        if ($( "#test2" ).parents().css('display') == 'none') {
    //iterate through each parent of the selected item  
            $( "#test2" ).parents().each(function () {
    //only change the display value if the element has a display:none;
                if ($(this).css('display') == 'none') {
                    $(this).css('display', 'block');
                    alert($('#test2').width());//or whatever we want to do with this number
    //reset values here       
                    $(this).css('display', 'none');
               }
            });
        }
    //showing as 100%
        alert($('#test2').width());
    });
    

提交回复
热议问题