为了账号安全,请及时绑定邮箱和手机立即绑定

为什么javascript this.style [property]返回一个空字符串?

为什么javascript this.style [property]返回一个空字符串?

莫回无 2019-10-21 15:19:25
为什么要this.style[property]得到一个空字符串?我的代码是:<!DOCTYPE html><html><head>    <title>Demo</title>    <style type="text/css">        #test{            height:100px;        }        .tclass{            width:100px;        }    </style>    <script type="text/javascript">        function $(ID){            var element=document.getElementById(ID||'nodId');            if(element){                element.css=css;            }            return element;        }        function css(prop,value){            if(value==null){                return this.style[prop];            }            if(prop){                this.style[prop]=value;            }            return true;        }        window.onload=function(){            var element=$("test");            alert(element.css("height")+","+element.css("width")+","+element.css("background"));            //alert ,,#ccc        };    </script></head><body>    <div id="test" style="background:#CCC;" class="tclass">Test</div></body></html>此代码警报,,#ccc,但我想得到100px,100px,#ccc,我怎么了?谁能帮我?更新我更改了CSS函数,现在可以正常使用了:    function css(prop,value){        if(value==null){            var b = (window.navigator.userAgent).toLowerCase();            var s;            if(/msie|opera/.test(b)){                s = this.currentStyle            }else if(/gecko/.test(b)){                s = document.defaultView.getComputedStyle(this,null);            }            if(s[prop]!=undefined){                return s[prop];            }            return this.style[prop];        }        if(prop){            this.style[prop]=value;        }        return true;    }
查看完整描述

3 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

该.style属性用于获取直接放置在元素上的样式。它不会从样式表中计算样式。



查看完整回答
反对 回复 2019-10-21
  • 3 回答
  • 0 关注
  • 637 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信