区分width()、css('width')、innerWidth()
#widthTest1 {
width: 200px;
height: 200px;
background-color: #00CCFF;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
border: 5px solid red;
}
#widthTest2 {
margin-top: 30px;
width: 200px;
height: 200px;
background-color: #00CCFF;
padding: 10px;
border: 5px solid red;
}
<div id="widthTest1">width test1</div>
<div id="widthTest2">width test2</div>

$(function(){
// .width()总是返回内容宽度,不管CSS box-sizing属性值.
// 截至jQuery 1.8,这可能需要检索的CSS的宽度加加上box-sizing的属性,
// 然后当元素有 box-sizing: border-box时候,减去个元素上任何潜在border和padding值。
// 为了避免这种问题,使用.css( "width" )而非.width()。
console.log('widthTest1 .width()'+$('#widthTest1').width()); // 170
console.log('widthTest2 .width()'+$('#widthTest2').width()); //
//第一个内容宽度是170 第二个内容宽度是200 两者主要区别是box-sizing:border-box;
// innerWidth() 包括padding,但是不包括border。
console.log('widthTest1 .width()'+$('#widthTest1').innerWidth()); // 190 = 200 - 5*2
console.log('widthTest1 .width()'+$('#widthTest2').innerWidth()); // 220 = 200 + 20
console.log('widthTest1 css("width")'+$('#widthTest1').css('width')); // 200px
console.log('widthTest2 css("width")'+$('#widthTest2').css('width')); // 200px
});
区分width()、css('width')、innerWidth()的更多相关文章
- JQuery .width()/.css("width")方法 比较
1. 获取到的值的区别 获取到的为实际宽度,不包括 内边距 和 边框: <div id="aa"> ...... </div> // 1. width() ...
- width() innerwidth() outerwidth() css('width')
不多说,用一图足以说明 首先先解释下普通元素和非普通元素, 非普通元素是指window,document这些 元素对象, 普通元素是指除window,document之外的元素,如:div 对于普通的 ...
- jQuery.width()和jQuery.css('width')的区别
[TOC] 问题描述 使用jQuery修改一个div的宽度时,发现$($0).width('10rem')总是修改成不正确的值,然后使用$($0).css('width', '10rem')时却能正确 ...
- css width="100" style ="width:100px" 区别
1. width="100"是正确的,而 width="100px"是错误的, style = "width:100px"是正确的 2. s ...
- JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别
整理翻译自:http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/ 大意是: 在J ...
- 详解 $().css('width')和$().width()的区别
在本次项目开发中,经常用jquery获取高度和宽度并且动态加载,有时候用$().css('width')或$().width()这两个方法获取宽度并设置,但是有时候出现获取不到的情况,查阅资料后发现他 ...
- jquery .width和css("width", )区别
1.$.fn.width会根据是否是borderBox来计算新的宽度,如果是borderBox,会额外加上padding和border的宽度,计算时只是按照px来,用rem做单位会出错: 2.$.fn ...
- css var & auto width css triangle
css var & auto width css triangle https://codepen.io/xgqfrms/pen/PooeEbd css var https://codepen ...
- offsetHeight/Width clientHeight/Width scrollHeight/Width等高宽算法
图解: jquery里的对应取法: clientHeight/Width:innerHeight/Width(), offsetHeight/Width: outerHeight/Width(). w ...
随机推荐
- ASP.NET-FineUI开发实践-13(二)
1.加图标和事件 上次已经通过DataSimulateTreeLevelField属性控制了树节点的显示,不用看也知道就是给指定列数据前面加个图标的HTML 可以在SimulateTreeHeper类 ...
- 关于 rem 作为单位设置大小
rem是相对长度单位.相对于根元素(即html元素)font-size计算值的倍数htm{font-size: 62.5%;}根元素(html)先设置一个font-size,一般情况下为了容易计算re ...
- L-value 和 R-value.
An L-value is something that can appear on the left side of an equal sign, An R-value is something t ...
- 【转】Auto Layout 进阶
原文:http://blog.csdn.net/ysy441088327/article/details/12558097 引言: Auto Layout是iOS6发布后引入的一个全新的布局特性, ...
- Swift - 14 - 字符串的基础操作
//: Playground - noun: a place where people can play import UIKit // 拼接 var str = "Hello, playg ...
- Java学习----反复做某件事情
for循环: public class TestFor{ public static void main(String[] args){ for(int x = 1; x < 3; x++) { ...
- 利用Php ssh2扩展实现svn自动提交到测试服务器
1.安装ssh2扩展 (1)window . 下载 php extension ssh2 下载地址 http://windows.php.net/downloads/pecl/releases/ssh ...
- sizeof(int *) 和 sizeof(int)型的大小问题
小问题,暂时记录注意一下 printf("sizeof(int): %d\n", (int)sizeof(int)); printf("sizeof(int ...
- 【新手--android日记】实现IOS风格电话界面
[前言--新手日记] 开始学习android开发,通过做一个通讯录练习,打算实现各种自己想实现的功能. 新手作品,技术含量很浅.主要是记录自己的学习过程. 纯学习之用,求评论,求建议,求教导. [正题 ...
- Codeforces Round #313 A Currency System in Geraldion
A Currency System in Geraldion Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64 ...