JavaScript或者jQuery怎样获取一个隐藏元素的宽高?
阿神
阿神 2017-04-11 12:38:01
[JavaScript讨论组]

怎样获取一个display:none的元素的宽高?

补充问题【按照以下代码测试】



    
    
    
    
    



    

阿神
阿神

闭关修行中......

全部回复(4)
迷茫

透明度也可以隐藏 不影响高度

大家讲道理

刚测试了下,jquery支持隐藏元素获取height和width

https://jsfiddle.net/9jor2mdr/1/

PHP中文网

$(selector).height()

http://stackoverflow.com/ques... 之前在jquery>=1.4.4版本,在你描述的情况下也是能获取到height的

http://jsfiddle.net/GALc7/1/

// 1.4.4以下版本的解决方案,先设置visibility属性,让其占位
$('#m1').css({'visibility': 'hidden', 'display': 'block'});
console.log($('#m2').height());
$('#m1').css({'display': 'none'});
黄舟

注意看题了,楼主肯定是设置了display:none的,不要答非所问,什么使用visibility来设置呀啥的,我要是用visibility设置了还需要问怎样获取宽高?下面给一个获取隐藏元素宽高的,代码如下:

;( function ( $ ){
  $.fn.addBack = $.fn.addBack || $.fn.andSelf;

  $.fn.extend({

    actual : function ( method, options ){
      // check if the jQuery method exist
      if( !this[ method ]){
        throw '$.actual => The jQuery method "' + method + '" you called does not exist';
      }

      var defaults = {
        absolute      : false,
        clone         : false,
        includeMargin : false,
        display       : 'block'
      };

      var configs = $.extend( defaults, options );

      var $target = this.eq( 0 );
      var fix, restore;

      if( configs.clone === true ){
        fix = function (){
          var style = 'position: absolute !important; top: -1000 !important; ';

          // this is useful with css3pie
          $target = $target.
            clone().
            attr( 'style', style ).
            appendTo( 'body' );
        };

        restore = function (){
          // remove DOM element after getting the width
          $target.remove();
        };
      }else{
        var tmp   = [];
        var style = '';
        var $hidden;

        fix = function (){
          // get all hidden parents
          $hidden = $target.parents().addBack().filter( ':hidden' );
          style   += 'visibility: hidden !important; display: ' + configs.display + ' !important; ';

          if( configs.absolute === true ) style += 'position: absolute !important; ';

          // save the origin style props
          // set the hidden el css to be got the actual value later
          $hidden.each( function (){
            // Save original style. If no style was set, attr() returns undefined
            var $this     = $( this );
            var thisStyle = $this.attr( 'style' );

            tmp.push( thisStyle );
            // Retain as much of the original style as possible, if there is one
            $this.attr( 'style', thisStyle ? thisStyle + ';' + style : style );
          });
        };

        restore = function (){
          // restore origin style values
          $hidden.each( function ( i ){
            var $this = $( this );
            var _tmp  = tmp[ i ];

            if( _tmp === undefined ){
              $this.removeAttr( 'style' );
            }else{
              $this.attr( 'style', _tmp );
            }
          });
        };
      }

      fix();
      // get the actual value with user specific methed
      // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc
      // configs.includeMargin only works for 'outerWidth' and 'outerHeight'
      var actual = /(outer)/.test( method ) ?
        $target[ method ]( configs.includeMargin ) :
        $target[ method ]();

      restore();
      // IMPORTANT, this plugin only return the value of the first element
      return actual;
    }
  });
})(jQuery);

这个是基于jquery写的,调用方式:

$('.hidden').actual('width');

官网地址:http://dreamerslab.com/works

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号