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

jer*_*iuh 1 css jquery

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

#container {
  position:relative;
  overflow:hidden;
  width:200px;
  height:200px;
}
Run Code Online (Sandbox Code Playgroud)

为什么这样:

alert('height is ' + $("#container").attr('height'));
Run Code Online (Sandbox Code Playgroud)

返回高度未定义?

谢谢.

tva*_*son 6

您可能想要使用cssheight方法.

$('#container').css('height')
Run Code Online (Sandbox Code Playgroud)

要么

$('#container').height();
Run Code Online (Sandbox Code Playgroud)

取决于你想要用它做什么.有关差异,请参阅参考文档.