开发者

attr('height') undefined in jQuery

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜