美文网首页
js终极获取修改样式

js终极获取修改样式

作者: 瓩千瓦 | 来源:发表于2019-03-02 11:06 被阅读0次

    function setGetStyle(obj,st){

        if(st instanceof Array){

            //instanceof:判断一个实例化是否属于某一对象;

            var getCss='';

            for(var i=0;i<st.length;i++){

                obj.currentStyle ? getCss+=obj.currentStyle[st[i]] : getCss+=window.getComputedStyle(obj,null)[st[i]];

            }

            return getCss;

        }else{

            for(var key in st){

                if(isNaN(st[key])){

                    obj.style[key]=st[key];

                }else{

                    obj.style[key]=st[key]+'px';

                }

            }

        }

    }

    //调用方法

    console.log(setGetStyle(box,['width']));//获取样式

    setGetStyle(box,{color:'red'});//添加设置修改样式

相关文章

网友评论

      本文标题:js终极获取修改样式

      本文链接:https://www.haomeiwen.com/subject/kxleuqtx.html