[jQuery]无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案
在做茶城网改版工作的时候,又遇到一个新问题,我需要用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢工了。
- 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)的新解决方案的更多相关文章
- jQuery无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案
		用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢工了. 用Chrome自带的开发工具一查,发现罢工 ... 
- 使用 jQuery 选择器获取页面元素,然后利用 jQuery 对象的 css() 方法设置其 display 样式属性,从而实现显示和隐藏效果。
		查看本章节 查看作业目录 需求说明: 使用 jQuery 选择器获取页面元素,然后利用 jQuery 对象的 css() 方法设置其 display 样式属性,从而实现显示和隐藏效果. 具体要求如下: ... 
- js获取隐藏元素宽高的方法
		网上有一些js获取隐藏元素宽高的方法,但是可能会存在某些情况获取不了. 例如: <!DOCTYPE html> <html lang="en"> <h ... 
- selenium+python自动化104-如何获取隐藏元素text文本
		前言 首先 selenium 是可以定位到隐藏元素的,但是 selenium 不能跟隐藏元素交互,也就是隐藏元素element不能使用element.click()方法. 隐藏元素element.te ... 
- 转:Jquery如何获取某个元素前(后)的文本内容?
		原文:[解决]Jquery如何获取某个元素前(后)的文本内容? <span> text here... <a id="target_element">百万创 ... 
- 使用 jQuery 选择器获取页面元素后,利用 jQuery 对象的 css() 方法设置其样式。
		查看本章节 查看作业目录 需求说明: 使用 jQuery 选择器获取页面元素后,利用 jQuery 对象的 css() 方法设置其样式. 要求如下: 点击页面的"更改样式"按钮后, ... 
- [SoapUI]怎样获取隐藏元素的文本内容Get text of hidden element
		隐藏元素无法通过gettext()获取其文本内容,须用javascript来获取 String actualDataPointName = (String) ((JavascriptExecutor) ... 
- JavaScript获取html元素的实际宽度和高度
		一.JavaScript获取html元素宽高 1.宽高都写在样式表里,就比如#div1{width:120px;}.这中情况通过#div1.style.width拿不到宽度,而通过#div1.offs ... 
- jquery中获取相邻元素相关的命令:next()、prev()和siblings()
		jquery里我们要获取某个元素的相邻元素时,可以用到的命令有三个: next():用来获取下一个同辈元素. prev():用来获取上一个同辈元素. siblings():用来获取所有的同辈元素. 下 ... 
随机推荐
- 棋盘覆盖(大数阶乘,大数相除 + java)
			棋盘覆盖 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的 ... 
- 擦肩而过的那块牌--记ACM_ICPC西安赛区现场赛
			说了那么多次orz,这次是真的orz了.去了西安打区域赛,也想过会打铁.但当终于那一刻确定打铁了之后.心里还是非常不开心的,颁奖的时候思考熊那家伙嚣张的举起来手中那个金杯,说实话闪到我眼了(太亮了QA ... 
- vmware tools 安装
			转到虚拟机 > 安装 VMware Tools(或 VM > 安装 VMware Tools).注意:如果您运行的是轻量版的 Fusion.不带 VMware Tools 的 Workst ... 
- 开始我的.NET的学习旅程
			今天开始了我的.NET学习之旅,终于弄懂了.NET与C#的关系,一开始还以为它们就是一个东西,原来不是那样的,C#只是基于.NET平台环境下运行的一种语言,.NET不止可以运行C#语言,更可以运行其他 ... 
- WeixinJSBridge:微信浏览器内置JavaScript 对象
			微信公众平台开始支持前端网页,大家可能看到很多网页上都有分享到朋友圈,关注微信等按钮,点击它们都会弹出一个窗口让你分享和关注,这个是怎么实现的呢?今天就给大家讲解下如何在微信公众平台前端网页上添加分享 ... 
- jquery .net 无刷新多文件上传
			Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例时php版本的,本文将详细介绍Uploadify在Aspnet中的使用,您也可以点击下面的链接进行演示 ... 
- SQL事务机制
			一.事务概念 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作时事务是作为最小 ... 
- C#常用的内置委托
			using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ... 
- 关于淘宝的数据来源,针对做淘宝客网站的淘宝api调用方法
			上次写了个淘宝返利模式的博客,直接被移除首页,不知道何故啊.可能是真的跟技术不太刮边. 众所周知,能够支撑一个网站运营的最基础不是程序写的多么好.也不是有多么牛X的运营人员,最主要的是数据,如果没有数 ... 
- 怎么查看mysql执行过的sql。
			有些时候当程序做了更新,数据库负载突然上来,或者并发翻了几倍.这个时候如果用show full processlist; 根本看不到完全的sql.怎么才能看是哪些sql导致的呢,在网上查了资料,有一下 ... 
