获取元素css样式的方法
 
元素.style.属性 
  只能获取行内样式 getComputedStyle (元素).属性名(IE不兼容) 
  getComputedStyle是window下一个方法;把对应的元素传给这个方法他是window上的一个属性获取浏览器计算的样式如果是复合属性名,把-去掉,换成驼峰命名;`console.log(getComputedStyle(元素).属性名)```console.log(getComputedStyle(box).width)```console.log(getComputedStyle(box)[‘width’])`` currentStyle(只有IE兼容) 
  元素.currentStyle.属性名IE8及以下,用这个方法获取元素css中设置的样式;他是元素身上的属性  
function getCss(curEle, attr){ 
  if('getComputedStyle' in window){
    return getComputedStyle(curEle)[attr]
  }
  else {
    return curEle.currentStyle[attr]
  }
}
---------------------------------------------
  function getCss(curEle, attr) {
  try {
    return getComputedStyle(curEle)[attr]
  }catch(e){
    return curEle.currentStyle[attr]
  }
}
 
例子 
<body>
    <div id="box" style="width:800px"></div>
    <script>
        let box = document.getElementById('box');
        
        
        
        
            
            
    
    
    
    
        
            
            
        
        console.log('getComputedStyle' in window);
        
        function getCss(curEle, attr){ 
          	let val = null;
            if('getComputedStyle' in window){
                val = getComputedStyle(curEle)[attr]
            }
            else {
                val = curEle.currentStyle[attr]
            }
          	let reg = /^(width|height|top|bottom|left|right|padding|margin|fontSize)&/
            if (reg.test(attr)) {
                val = parseFloat(val)
            }
            return val
        }
        console.log(getCss(box, 'width'))
    </script>
</body>
 
封装获取和设置css的方法
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        #box {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        
        function getCss(curEle, attr) {
            let val = null
            if ('getComputedStyle' in window) {
                val = getComputedStyle(curEle)[attr]
            } else {
                val = curEle.currentStyle[attr]
            }
            let reg = /^(width|height|top|bottom|left|right|padding|margin|fontSize)$/
            if (reg.test(attr)) {
                val = parseFloat(val)
            }
            return val
        }
        function setCss(curEle, attr, val) {
            let reg = /^(width|height|top|bottom|left|right|padding|margin|fontSize)$/
            if (reg.test(attr)) {
                if (typeof val === 'number') {
                    val = val + 'px'
                }
            }
            curEle.style[attr] = val
        }
        
        
        
        
        function setGroupCss(curEle, obj) {
            for (key in obj) {
                
                setCss(curEle, key, obj[key])
            }
        }
        
        
        
        
        
        function css() {
            let curEle = arguments[0];
            let attr = arguments[1];
            let val = arguments[2];
            
            if (arguments.length === 2) {
                if (typeof attr === 'string') {
                    
                    return getCss(curEle, attr)
                } else {
                    setGroupCss(curEle, attr)
                }
            }
            
            else if (arguments.length === 3) {
                setCss(curEle, attr, val)
            }
        }
    </script>
</body>
</html>
                
                
                
        
    
转载请注明原文地址:https://ipadbbs.8miu.com/read-60142.html