亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么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 關注
  • 704 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號