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

javascript - jQuery Div attr (width = $('#immagineCont').attr('clientWidth'); = undefined - Stac

programmeradmin0浏览0评论

im no expert javascript coder. i used like 5 hours now trying alot of different stuff. and i noticed some things that i cant figure out im trying to get this to work

width = $('#immagineCont').attr('clientWidth');

but it returns undefined i tried doing

width = $('#immagineCont').attr('id');

witch returned #immagineCont i guess is correct.

when i use

 $('#immagineCont')

i get this in firebug 1) 2)

so what im trying to do is get the damn clientWidth witch seems impossible. i even tryed children("0") and children("#0") im going blow my brains soon ^^

im no expert javascript coder. i used like 5 hours now trying alot of different stuff. and i noticed some things that i cant figure out im trying to get this to work

width = $('#immagineCont').attr('clientWidth');

but it returns undefined i tried doing

width = $('#immagineCont').attr('id');

witch returned #immagineCont i guess is correct.

when i use

 $('#immagineCont')

i get this in firebug 1) http://bildr.no/view/1325119 2) http://bildr.no/view/1325120

so what im trying to do is get the damn clientWidth witch seems impossible. i even tryed children("0") and children("#0") im going blow my brains soon ^^

Share Improve this question asked Nov 23, 2012 at 12:12 coldasicecoldasice 691 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

attr would only work if there was an attribute with this name.

You may simply use

width = $('#immagineCont').get(0).clientWidth;

but you more probably need

width = $('#immagineCont').width();

which is more reliable when you want to do something that works the same across browsers (but doesn't include padding and borders).

You might also be interested by outerWidth.

A note about what happens and the need for get(0) : a jQuery collection (like $('#immagineCont')) wraps one or more standard DOM objects that you can get using get(i) or [i]. If you want to access native properties of the DOM object, when jQuery doesn't offer a proxy function, you need to get this native object first.

Use jQuery width() for this purpose:

var width = $('#immagineCont').width();

Also there is no attribute set to the element called clientWidth. This is a javascript property so it can be retrieved by dereferencing the jQuery object:

var width = $('#immagineCont')[0].clientWidth;
发布评论

评论列表(0)

  1. 暂无评论