开发者

attr('height') undefined in jQuery

开发者 https://www.devze.com 2022-12-25 05:40 出处:网络
I have a container div with the follo开发者_Go百科wing CSS: #container { position:relative; overflow:hidden;

I have a container div with the follo开发者_Go百科wing CSS:

#container {
  position:relative;
  overflow:hidden;
  width:200px;
  height:200px;
}

Why does this:

alert('height is ' + $("#container").attr('height'));

Return that height is undefined?

Thanks.


You probably want to use the css or height methods.

$('#container').css('height')

or

$('#container').height();

depending on what you are trying to do with it. See the referenced documentation for the differences.


Try:

$("#container").height()

It is due to the height being in the css and not an actual attribute on the html tag.


jQuery.attr refers to HTML attributes not CSS properties. Use .css("height") or mentioned .height() method.

More about those methods:

  1. jQuery.css()
  2. jQuery.attr()
  3. jQuery API Reference
0

精彩评论

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