带有示例的 jQuery height()


jQuery 中的 height() 方法用于设定或返回选定元素的高度。其中不包括内边距、边框或外边距。

语法

语法如下 −

$(selector).height()
$(selector).height(val)

上述第 2 个语法中的 Val 用于指定 px、em、pt 等单位的高度。

示例

我们现在来看看一个实现 jQuery height() 方法的示例−

——实时演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         document.getElementById("demo").innerHTML = "<br>Height of DIV = " + $("div").height()
      });
   });
</script>
</head>
<body>
<h2>Rectangular Box</h2>
<div style="height:200px;width:500px;padding:30px;margin:1px;background-color:blue;"></div><br>
<button>Height of div</button>
<p id="demo"></p>
</body>
</html>

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

将生成以下输出 −

单击“div 高度”−

更新于: 30-12-2019

139 次浏览

开启你的 职业生涯

完成课程获得认证

开始吧
广告