获取元素css样式的方法

    技术2025-10-15  4

    获取元素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){ // 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'); // 获取元素的css样式 // 1、元素.style.width 这是获取的行内样式 // console.log(box.style.width) '' // 获取不到 // 2、getComputedStyle // 1、他是window上的一个属性 // 2、getComputedStyle(元素).属性名 // console.log('getComputedStyle' in window) // true // console.log(getComputedStyle(box).width) // '100px' // console.log(getComputedStyle(box)['width']) // '100px' // console.log(getComputedStyle(box)['backgroundColor']) //'rbg(255,0,0)' // 3、 currentStyle // 1、这个属性只在IE下存在 // 2、元素.currentStyle.属性名 // console.log(box.currentStyle.width); console.log('getComputedStyle' in window); /* 封装一个获取scc样式的方法, */ function getCss(curEle, attr){ // 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 } // setCss(box, 'width', 200) // setCss(box, 'height', '200px') // setCss(box, 'backgroundColor', 'green') function setGroupCss(curEle, obj) { for (key in obj) { // key,属性名 setCss(curEle, key, obj[key]) } } // setGroupCss(box, { // width: 200, // height: 300, // backgroundColor: 'pink' // }) function css() { let curEle = arguments[0]; let attr = arguments[1]; let val = arguments[2]; // 如果arguments的legnth是2,那说明你不是想获取样式,就是想成组设置样式 if (arguments.length === 2) { if (typeof attr === 'string') { // 如果此条件成立,那就说明第二个参数就是字符串,就是想获取参数 return getCss(curEle, attr) } else { setGroupCss(curEle, attr) } } // 如果arguments的legnth是3,那说明你是想单个设置样式 else if (arguments.length === 3) { setCss(curEle, attr, val) } } </script> </body> </html>
    Processed: 0.009, SQL: 9