最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the difference between jQuery .width(), style.width, and jQuery .css('width')? - Sta

programmeradmin1浏览0评论

What is the difference between the values returned by .width() from jQuery, .css('width') from jQuery, and the .style.width attribute on a node?

I'm curious because, in trying to set the width of two table cells to be equal, the first two gave me the same wrong answer. What are they actually measuring, and why is it not the same as the value that I can see in the .style attribute when I view the element in a browser?

What is the difference between the values returned by .width() from jQuery, .css('width') from jQuery, and the .style.width attribute on a node?

I'm curious because, in trying to set the width of two table cells to be equal, the first two gave me the same wrong answer. What are they actually measuring, and why is it not the same as the value that I can see in the .style attribute when I view the element in a browser?

Share Improve this question edited Sep 8, 2014 at 4:11 bonCodigo 14.4k1 gold badge50 silver badges92 bronze badges asked Jul 5, 2013 at 18:26 ckerschckersch 7,6872 gold badges40 silver badges48 bronze badges 3
  • Yeah that above needs to be deleted but it won't let me for some strange reason – iConnor Commented Jul 5, 2013 at 18:28
  • Check this jsfiddle: jsfiddle.net/EjPBy – A. Wolff Commented Jul 5, 2013 at 18:41
  • Just modified @A.Wolff's code to show the widths, but for sytle.width, there's still notthing showed.. – bonCodigo Commented Sep 8, 2014 at 4:10
Add a comment  | 

5 Answers 5

Reset to default 8

From width official documentation

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .width() method is recommended when an element's width needs to be used in a mathematical calculation.

I'd say there is no difference between .css('width') and .style.with as css() method is used to set style object properties.

The difference between .css('width') / .width() and .style.with is that jQuery methods get the computed style using window.getComputedStyle( elem, null ). This method is used to read the actual property after CSS and styles are applied.

Complementing Claudio Redi's answer: see the difference live: http://jsfiddle.net/vovkss/kPXaB/

I've used cm units intentionally to demonstrate that the output of .css('width') may be converted to pixels (or other units), while .width() is always an integer value representing a number of pixels.

Note that .style.width only applies to inline styles.

  • HTML:

    <div id="mydivInlineStyle" style="width: 10cm"><b>Inline style:</b>
    </div>
    
    <div id="mydivExternalStylesheet"><b>External stylesheet:</b> 
    </div>
    
  • CSS:

    div { 
      width: 10cm; 
      border: 10px solid blue; padding: 11px; margin: 12px; 
      background: aqua; white-space: pre; 
    }
    
  • JS:

    $('div').each(function(){ 
      $(this).append(
        'jQuery width(): ' + $(this).width() + "\n" + 
        '.css(\'width\'): ' + $(this).css('width') + "\n" + 
        '.style.width: ' + this.style.width
      ); 
    });
    
  • Output:

    Inline style: 
      jQuery width(): 378 
      .css('width'): 378px 
      .style.width: 10cm
    
    External stylesheet: 
      jQuery width(): 378 
      .css('width'): 378px 
      .style.width: 
    

There is also a difference in "intended width" vs. real width. .width() will give you the content width of an element, .css("width") will give you the width as intended by the style sheet.

.css('width') returns the attribute with the unit appended while .width() does not, it will give you the number in pixels.

So if you have an element with a width of 200px .css('width') will return "200px" while .width() will return 200.

.width() will give you what is applied .css('width') will give you what is in the style attribute

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论