前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >getComputedStyle currentStyle代表哪里的兼容

getComputedStyle currentStyle代表哪里的兼容

作者头像
贵哥的编程之路
发布2020-10-28 14:55:31
4860
发布2020-10-28 14:55:31
举报
文章被收录于专栏:用户7873631的专栏
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
         *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 100px;
            height: 100px;
            padding: 50px;
            border: 50px solid #000;
            background: red;
            background-clip: content-box;
        }
    </style>
</head>
<body>
    <div id="box">
        
    </div>
    <script type="text/javascript">
       /* var q=document.getElementById("box");
        //q.style.width="166px";
        //q.style.height="166px";
        //另一种方法获取宽高。 1.通过getComputedStyle获取宽高
        var style1=getComputedStyle(q);
        //style1.width="100px";
        //style1.height="100px";
        console.log(style1.width);
        console.log(style1.height);
        */
    /*
    2.通过currentStyle属性获取宽高
    2.1获取的宽高不包括 边框和内边距
    2.2即可以获取行内设置的宽高也可以获取CSS设置的宽高
    2.3只支持获取, 不支持设置
    2.4只支持IE9以下浏览器
    */
   /*ar q=document.getElementById("box");
   /*style1.width = "166px";
    style1.height = "166px";*/
   /*var style1=q.currentStyle;
   
   console.log(style1.width);
   console.log(style1.height);*/
/*
    3.通过style属性获取宽高
    3.1获取的宽高不包括 边框和内边距
    3.2只能获取行内设置的宽高, 不能获取CSS设置的宽高
    3.3可以获取也可以设置
    3.4高级低级浏览器都支持
    */
  /*var oDiv = document.getElementById("box");
   oDiv.style.width="666px";
   oDiv.style.height="666px";
   console.log(oDiv.style.width);
    console.log(oDiv.style.height);*/
     /*
    4.offsetWidth和offsetHeight
    4.1获取的宽高包含 边框 + 内边距 + 元素宽高
    4.2即可以获取行内设置的宽高也可以获取CSS设置的宽高
    4.3只支持获取, 不支持设置
    4.4高级低级浏览器都支持
    */
    var oDiv = document.getElementById("box");
    oDiv.offsetWidth="166px";
    oDiv.offseHeight="166px";
   /* oDiv.style.width = "66px";
    oDiv.style.height = "66px";*/
    console.log(oDiv.offsetWidth);
    console.log(oDiv.offsetHeight);
     /*
    1.getComputedStyle/currentStyle/style
    获取的宽高不包括 边框和内边距
    2.offsetWidth/offsetHeight
    获取的宽高包括 边框和内边距

    3.getComputedStyle/currentStyle/offsetXXX
    只支持获取, 不支持设置
    4.style
    可以获取, 也可以设置

    5.getComputedStyle/currentStyle/offsetXXX
    即可以获取行内,也可以获取外链和内联样式
    6.style
    只能获取行内样式
    */
    </script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/09/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档