attr('height')在jQuery中未定义

时间:2010-03-26 16:32:29

标签: jquery css

我有一个带有以下CSS的容器div:

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

为什么这样:

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

返回高度未定义?

感谢。

3 个答案:

答案 0 :(得分:6)

您可能希望使用cssheight方法。

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

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

取决于您尝试使用它做什么。有关差异,请参阅参考文档。

答案 1 :(得分:4)

尝试:

$("#container").height()

这是由于高度在css中,而不是html标签上的实际属性。

答案 2 :(得分:2)

jQuery.attr是指HTML属性而不是CSS属性。使用.css("height")或提及.height()方法。

有关这些方法的更多信息:

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