在做茶城网改版工作的时候,又遇到一个新问题,我需要用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢工了。

 
  用Chrome自带的开发工具一查,发现罢工的Tab中。小插件一些重要元素的宽度都变成“0”了,因为这个小插件需要计算动态宽度来实现,于是马上想到是小插件中的宽度获取失败了,果不其然。
 
  汗,居然一直没发现jQuery无法获取隐藏元素(display:none)的宽度(width)和高度(height),为了兼容IE6,我用1.x版,而且是官方最新的1.10.2版,不知道在2.x版中这个问题有没有解决。
 
  既然jQuery都不支持,那么Javascript也就肯定不支持了,网上搜索了一下,有个解决方案是用visibility:hidden来代替display:none,由于visibility:hidden占用物理空间,因此可以获取宽高。
 
  问题是这需要我去改动已经写好的Tab插件,这种拆东墙补西墙的事情,总会让人感觉不爽的。长时间搜索其它解决方案无果,就在我快要妥协的时候,突然眼前一亮,发现了个好东西:jQuery Actual
 
  可以说它几乎完美的解决了这个问题,而且使用方法极其简单,使用$('#someElement').actual('width')的方式来代替$('#someElement').width()就可以了,兼容性也十分出色,可以兼容IE6浏览器,压缩后体积只有1.1K也是一大亮点,更牛的是还支持inner和outer的计算。
官方信息
 
jQuery版本要求
  • jQuery 1.2.3+
 
所兼容的浏览器
  • Firefox 2.0+
  • Internet Explorer 6+
  • Safari 3+
  • Opera 10.6+
  • Chrome 8+
安装方法
  • HTML文档需要声明DOCTYPE
  • 引用JS文件即可
    <script type="text/javascript" src="path/jquery.min.js"></script>
    <script type="text/javascript" src="path/jquery.actual.js"></script>
使用方法
  • 代码范例
  • //get hidden element actual width
    $('.hidden').actual('width'); //get hidden element actual innerWidth
    $('.hidden').actual('innerWidth'); //get hidden element actual outerWidth
    $('.hidden').actual('outerWidth'); //get hidden element actual outerWidth and set the `includeMargin` argument
    $('.hidden').actual('outerWidth',{includeMargin:true}); //get hidden element actual height
    $('.hidden').actual('height'); //get hidden element actual innerHeight
    $('.hidden').actual('innerHeight'); //get hidden element actual outerHeight
    $('.hidden').actual('outerHeight'); // get hidden element actual outerHeight and set the `includeMargin` argument
    $('.hidden').actual('outerHeight',{includeMargin:true}); //if the page jumps or blinks, pass a attribute '{ absolute : true }'
    //be very careful, you might get a wrong result depends on how you makrup your html and css
    $('.hidden').actual('height',{absolute:true}); // if you use css3pie with a float element
    // for example a rounded corner navigation menu you can also try to pass a attribute '{ clone : true }'
    // please see demo/css3pie in action
    $('.hidden').actual('width',{clone:true});

    个人觉得jQuery官方应该考虑将这个功能写进内核,那就更方便了。为了防止以后jQuery Actual的官网打不开(现在就时不时会和谐)或者下载不了,在这里存一份jquery.actual.js的源码,以备不时只需。

  • 源码:jquery.actual.js

    ;( 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
    }; 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: block !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 (){
    var $this = $( this ); // Save original style. If no style was set, attr() returns undefined
    tmp.push( $this.attr( 'style' ));
    $this.attr( '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 );

    此上是转载别人的文章。

  • 此外我还发现,如果觉得上述方法未曾听过或者不知道,但是要知道原因,为什么会取不到高度,这是今天一直困扰我的问题。因为我在获取高度之前,对前面的元素添加了隐藏的动作,我用的是hide(),但是在页面的标签上就会形成display:none;这个动作,所以之后我再取值就得不到了。读了上述之后,瞬间明白。jQuery无法获取隐藏元素(display:none)的宽度(width)和高度(height),解决方案是用visibility:hidden来代替display:none,由于visibility:hidden占用物理空间,因此可以获取宽高。

[jQuery]无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案的更多相关文章

  1. jQuery无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案

    用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢工了. 用Chrome自带的开发工具一查,发现罢工 ...

  2. 使用 jQuery 选择器获取页面元素,然后利用 jQuery 对象的 css() 方法设置其 display 样式属性,从而实现显示和隐藏效果。

    查看本章节 查看作业目录 需求说明: 使用 jQuery 选择器获取页面元素,然后利用 jQuery 对象的 css() 方法设置其 display 样式属性,从而实现显示和隐藏效果. 具体要求如下: ...

  3. js获取隐藏元素宽高的方法

    网上有一些js获取隐藏元素宽高的方法,但是可能会存在某些情况获取不了. 例如: <!DOCTYPE html> <html lang="en"> <h ...

  4. selenium+python自动化104-如何获取隐藏元素text文本

    前言 首先 selenium 是可以定位到隐藏元素的,但是 selenium 不能跟隐藏元素交互,也就是隐藏元素element不能使用element.click()方法. 隐藏元素element.te ...

  5. 转:Jquery如何获取某个元素前(后)的文本内容?

    原文:[解决]Jquery如何获取某个元素前(后)的文本内容? <span> text here... <a id="target_element">百万创 ...

  6. 使用 jQuery 选择器获取页面元素后,利用 jQuery 对象的 css() 方法设置其样式。

    查看本章节 查看作业目录 需求说明: 使用 jQuery 选择器获取页面元素后,利用 jQuery 对象的 css() 方法设置其样式. 要求如下: 点击页面的"更改样式"按钮后, ...

  7. [SoapUI]怎样获取隐藏元素的文本内容Get text of hidden element

    隐藏元素无法通过gettext()获取其文本内容,须用javascript来获取 String actualDataPointName = (String) ((JavascriptExecutor) ...

  8. JavaScript获取html元素的实际宽度和高度

    一.JavaScript获取html元素宽高 1.宽高都写在样式表里,就比如#div1{width:120px;}.这中情况通过#div1.style.width拿不到宽度,而通过#div1.offs ...

  9. jquery中获取相邻元素相关的命令:next()、prev()和siblings()

    jquery里我们要获取某个元素的相邻元素时,可以用到的命令有三个: next():用来获取下一个同辈元素. prev():用来获取上一个同辈元素. siblings():用来获取所有的同辈元素. 下 ...

随机推荐

  1. java学习之i/o

    基本的输入输出  这里有两种方法,其中一种被注释了,可以换着注释后测试一遍 class TestSystem { public static void main(String[] args) { /* ...

  2. ASP.NET 开发框架汇总

    先简单记录一下,以后慢慢添加 1.ASP.NET Aries 2.ASP.NET DevExpress

  3. C# 和Java的foreach的不同用法

    循环语句为苦逼的程序猿们提供了很大的便利,有while.do...while.for和 foreach.而且foreach语句很简洁,但是它的优点不仅仅在于此,它的效率也是最高的. 作为两个开发语言, ...

  4. MVC和传统的以模板为中心的web架构比较

    特性 以模板为中心 MVC架构 页面产生方式 运行并替换标签中的语句 由模板引擎生产HTML页面 路径解析 映射到文件系统路径,也可以通过rewrite等技术来重定向 由控制器定义,并可以通过路由系统 ...

  5. C#调用SAPWebService

    其实和调用其它WebService 没有很大不同  只是不了解SAP的人 可能不太明白 SAP接口中的相关参数 //调用接口 , 创建对象 ServiceReference1.Z_IF_MM_VEND ...

  6. .NET进阶系列之一:C#正则表达式整理备忘

    有一段时间,正则表达式学习很火热很潮流,当时在CSDN一天就能看到 好几个正则表达式的帖子,那段时间借助论坛以及Wrox Press出版的<C#字符串和正则表达式参考手册>学习了一些基础的 ...

  7. hdu2429Ping pong

    Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider th ...

  8. java static关键字

    方便在没有创建对象的情况下来进行调用(方法/变量). 很显然,被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载了,就可以通过类名去进行访问. static可以用来修饰类的 ...

  9. SpringMVC(一) —— 入门

    SpringMVC原理图: 步骤: 首先用户发送请求.——>DispatcherServlet,前端控制器收到请求后自己不进行处理,而是委托给其他的解析器进行处理,作为统一访问点,进行全局的流程 ...

  10. H - Ones

    Description Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a ...