PS :height是指内容的高度 不包含padding margin…
innerheight 包含element+padding

代码如下:

HTML部分:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>CSS操作及jQuery的盒子模型</title>
    <script src="jquery-2.1.4.min.js"></script>
    <script src="CSS%20hezi.js"></script>
    <link rel="stylesheet" type="text/css" href="hezi.css">
    <style>
        #d1{
            width: 200px;
            height: 200px;
            padding: 30px;
            margin: 50px;
            border: 2px solid red;
            background-color: antiquewhite;
        }
    </style>
</head>
<body>
<div></div>
<div id="d1"></div>
</body>
</html>

CSS部分

.style{
     width: 100px;
     height: 100px;
     background-color: green;

 }

.style1{
    width: 100px;
    height: 100px;
    background-color: crimson;

}

Javascript部分

/**
 * Created by qing on 2015/8/20.
 */
$(document).ready(function () {
    //写法一
    //$("div").css("width","100px");
    //$("div").css("height","100px");
    //$("div").css("background-color","blue")

    //写法二
    //$("div").css({
    //    width:"100px",height:"100px",backgroundColor:"blue"
    //});

    //方法三
    $("div").addClass("style");

    $("div").click(function(){

        //$(this).addClass("style1");
        //$(this).removeClass("style"); //移除样式
        $(this).toggleClass("style1");

    });

    //alert($("#d1").height());//只包含element高度
    //alert($("#d1").innerHeight()); //包含element+padding(上下)
    //alert($("#d1").outerHeight());//element+padding(上下)+border(上下)
    alert($("#d1").outerHeight(true)); //包含全部的高度
});
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐