#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. CSS入门学习(转)

    一.基础学习 1.何为CSS CSS是Cascading Style Sheets(层叠样式表)的简称,是一种标记语言,它不需要编译,可以直接由浏览器执行(属于浏览器解释型语 言). CSS文件也可以 ...

  2. document.all用法

    document.all用法 一. document.all是页面内所有元素的一个集合.例如:       document.all(0)表示页面内第一个元素二.document.all可以判断浏览器 ...

  3. CSS基本知识介绍

    CSS (Cascading Style Sheet)叠层样式表.用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言. 样式的几种控制方法: 1.行内样式         <div ...

  4. Kill Processes in Linux

    Step 1: find processes to kill ps -ef | grep java Step 2: Kill the process based on process id kill ...

  5. iOS面试小题集锦

      1.Object-C有多继承吗?没有的话用什么代替? cocoa 中所有的类都是NSObject 的子类 多继承在这里是用protocol 委托代理 来实现的你不用去考虑繁琐的多继承 ,虚基类的概 ...

  6. CoreLocation+MapKit系统定位(含坐标以及详细地址)

    iOS8 之后出现一些新的配置 [self.manager requestWhenInUseAuthorization]; 并且在info.plist文件中增加 NSLocationWhenInUse ...

  7. [C++] namespace相关语法

    本段测试代码包括如下内容: (1) 如何访问namespace中声明的名称:(2) namespace导致的相关冲突:(3) namespace可嵌套:(4) 可以在namespace中使用using ...

  8. 【NOIP2014】赛后总结

    noip考完了,心中所牵挂的一下子就消散了,感觉浑身很轻松. 说实话,我参加noip有好几次了,这应该会是我的最后一次,尽管如此,无论是在考试的前几天还是在考试的时候,心中都没有太多的紧张. 我在no ...

  9. bootstrap弹出框居中

    1.html页面(如果效果出不来,注意修改单引号) <!DOCTYPE html> <html lang="zh-CN"> <head> < ...

  10. 使用Ajax.ActionLink时,点击对应的按钮会重新加载一个页面,而不是在当前页面的指定模块加载

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...