#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()的更多相关文章

  1. JQuery .width()/.css("width")方法 比较

    1. 获取到的值的区别 获取到的为实际宽度,不包括 内边距 和 边框: <div id="aa"> ...... </div> // 1. width() ...

  2. width() innerwidth() outerwidth() css('width')

    不多说,用一图足以说明 首先先解释下普通元素和非普通元素, 非普通元素是指window,document这些 元素对象, 普通元素是指除window,document之外的元素,如:div 对于普通的 ...

  3. jQuery.width()和jQuery.css('width')的区别

    [TOC] 问题描述 使用jQuery修改一个div的宽度时,发现$($0).width('10rem')总是修改成不正确的值,然后使用$($0).css('width', '10rem')时却能正确 ...

  4. css width="100" style ="width:100px" 区别

    1. width="100"是正确的,而 width="100px"是错误的, style = "width:100px"是正确的 2. s ...

  5. JQuery获取元素宽度.width()与.css(‘width’)两个函数的区别

    整理翻译自:http://blog.jquery.com/2012/08/16/jquery-1-8-box-sizing-width-csswidth-and-outerwidth/ 大意是: 在J ...

  6. 详解 $().css('width')和$().width()的区别

    在本次项目开发中,经常用jquery获取高度和宽度并且动态加载,有时候用$().css('width')或$().width()这两个方法获取宽度并设置,但是有时候出现获取不到的情况,查阅资料后发现他 ...

  7. jquery .width和css("width", )区别

    1.$.fn.width会根据是否是borderBox来计算新的宽度,如果是borderBox,会额外加上padding和border的宽度,计算时只是按照px来,用rem做单位会出错: 2.$.fn ...

  8. css var & auto width css triangle

    css var & auto width css triangle https://codepen.io/xgqfrms/pen/PooeEbd css var https://codepen ...

  9. offsetHeight/Width clientHeight/Width scrollHeight/Width等高宽算法

    图解: jquery里的对应取法: clientHeight/Width:innerHeight/Width(), offsetHeight/Width: outerHeight/Width(). w ...

随机推荐

  1. C#基础知识01(continue、break 和 return、ref 和 out)

    break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...

  2. Swift - 14 - 字符串的基础操作

    //: Playground - noun: a place where people can play import UIKit // 拼接 var str = "Hello, playg ...

  3. Swift - 05 - 数值型字面量

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  4. nodejs新手教程中upload file的问题

    可参见: http://cnodejs.org/topic/50234890f767cc9a51f88481 request.setEncoding("utf8");应注释掉.

  5. Linux Chaining Operators用法学习

    Linux Chaining Operators顾名思义,就是连接命令的操作,有些时候,往往一些命令可以用一行命令代替,我们就不需要大动干戈再去写Shell Script了,掌握和学习这些Chaini ...

  6. 百度上传插件WebUploader,angularjs指令封装

    1.WebUploader特点 官网地址:http://fex.baidu.com/webuploader/ 1.1 分片.并发 分片与并发结合,将一个大文件分割成多块,并发上传,极大地提高大文件的上 ...

  7. linux 配置 sphinx 全文搜索引擎

    因为公司网站需要,最近在弄sphinx搜索引擎,也是遇到各种问题,最终终于解决了. 服务器系统:centos7 (64位) 详情看安装官网的安装教程进行 coreseek 3.2.14 这里只提一些注 ...

  8. Js字符串判断

    判断字符串 str=''; if(!str) // str为null 或者"" 或者0

  9. jQuery1.6以上版本prop和attr的区别

  10. SCJP_104——题目分析(5)

    18. public class Test { public static void add3(Integer i) { int val=i.intvalue(); val+=3; i=new Int ...