我想使用脚本根据实际元素的高度将最小高度或最大高度添加到元素。
所以我得到了div的.height()(?),然后使用该值设置了最小/最大高度。这可能吗?它必须是动态的,以便div可以更改高度,但仍然具有正确的min-height或max-height。
这是您想要的示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Set a DIV height</title>
<style>
.box{
background: #f2f2f2;
border: 1px solid #ccc;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$(".set-height-btn").click(function(){
var newHeight = $(".input-height").val();
$(".box").height(newHeight);
});
});
</script>
</head>
<body>
<form>
<input type="text" class="input-height">
<button type="button" class="set-height-btn">Set Height</button>
<p>Enter the value in input box either as number (e.g. 100, 200) or combination of number and unit (e.g. 100%, 200px, 50em, auto) and click the "Set Height" button.</p>
</form>
<br>
<div class="box">This is simple DIV box</div>
</body>
</html>