热门关键字:
jquery > jquery教程 > jquery教程 > 如何使用jQuery动态设置div元素的高度

如何使用jQuery动态设置div元素的高度

371
作者:管理员
发布时间:2021/2/4 14:11:53
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3839
答:使用JavaScriptheight()方法
您可以<div>使用jQueryheight()方法动态设置框的高度。


该height()方法仅将元素的高度返回为无单位像素值。但是,调用该height(value)方法将设置元素的高度,其中值可以是字符串(例如100%,50px,25em,auto等)或数字。如果仅为该值提供数字,则jQuery会将其假定为像素单位。让我们尝试一个示例,看看它是如何工作的:


例试试这个代码»
<!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-3.5.1.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>




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:jQuery
友荐云推荐