开发者

How to get the value from css of "div" tag using jQuery?

Ex:

<div开发者_开发问答 id="h" style="background-image:url('images//line.jpg'); background-repeat: repeat-x; width:300px; height:25px; padding-left:10px; color:white;">Frame</div>

Now, I want to get the value of width from style using jQuery. What should be jQuery.


$('#h').width()   //  from jQuery dimensions (core)

or

$('#h').css('width');

.width() returns the calculated width. .css('width') just returns whatever you've written into the style. So in general, it's a better idea to stick with .width() unless you really want to know the actuall value.

If you need the width of an element in more "detail", check out the .innerWidth() and .outerWidth() methods from jQuery dimensions. Those calculate the width with padding, borders, padding etc.

Ref.: .width(), .css(), .outerWidth(), .innerWidth()


Use the css method:

$("#h").css("width");

Using the css method and the property name of width will give you the actual css result, as apposed to width() which will give a unitless result.

From the width docs:

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).


Use either

$('#h').width();

or

$('#h').css('width');

The latter returns the unit value (eg 300px), the former just a value (300).

See jQuery Width() API


The css method:

var width = $('#h').css('width');


You could use jQuery like this:

alert($("#h").css("width"));

To play a little with jQuery you should use...

http://jsfiddle.net/WEWVp/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜