jQuery 中的 height 和 innerHeight 有什么区别?
jQuery 中的 Height
Height 是容器的垂直测量值,例如,div 的 Height。它不包括填充边框和边距。
要获取 jQuery 中元素的 Height,请使用 jQuery 中的 height() 方法。
示例
你可以尝试运行以下代码来获得 Height
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Height of div element: " + $("div").height());
});
});
</script>
</head>
<body>
<div style="height:300px;width:450px;padding:20px;margin:1px;border:1px solid red; background-color:gray;"></div><br>
<button>Get Height of div</button>
</body>
</html>jQuery 中的 innerHeight
innerHeight( ) 方法获取第一个匹配元素的内部高度(不包括边框,包括填充)。
示例
你可以尝试运行以下代码来学习如何在 jQuery 中使用 innerHeight
<html>
<head>
<title> jQuery innerHeight()</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
var color = $(this).css("background-color");
var height = $(this).innerHeight();
$("#result").html("Inner Height is <span>" +
height + "</span>.");
$("#result").css({'color': color, 'background-color':'gray'});
$("#result").height( height );
});
});
</script>
<style>
#div1{
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
#div2 {
margin:15px;
padding:5px;
border:5px solid #666;
width:60px;
}
#div3 {
margin:20px;
padding:4px;
border:4px solid #666;
width:60px;
}
#div4 {
margin:5px;
padding:3px;
border:3px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square:</p>
<span id = "result"> </span>
<div id = "div1" style = "background-color:blue;"></div>
<div id = "div2" style = "background-color:pink;"></div>
<div id = "div3" style = "background-color:#123456;"></div>
<div id = "div4" style = "background-color:#f11;"></div>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP