脑子突然锈了,问一下如何通过样式名,获取背景色?

aiit 2018-02-01 09:47:32
比如说下面这个样式
.datagrid-row-selected {
background: #00bbee;
color: #fff;
}

...全文
285 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky993 2018-02-02
  • 打赏
  • 举报
回复
document.getElementsByName("datagrid-row-selected").style.background
伟洪winni 2018-02-02
  • 打赏
  • 举报
回复
class选择器
aiit 2018-02-02
  • 打赏
  • 举报
回复
万分感谢,正是我所要的
引用 5 楼 showbo 的回复:
遍历样式表

<style>
    .blue{color: Blue;}
    .red{color:red}
</style>
<script>
    function getCss(theClass, attr) {
        var sl = document.styleSheets.length;
        if (sl == 0) return;
        var cssRules = document.styleSheets[0].rules ? 'rules' : 'cssRules';
        for (var i = 0; i < sl; i++) {
            for (var j = 0, rl = document.styleSheets[i][cssRules].length; j < rl; j++) {
                if (document.styleSheets[i][cssRules][j].selectorText == theClass) {
                    return document.styleSheets[i][cssRules][j].style[attr]
                }
            }
        }
    }
    alert(getCss('.blue', 'color'))
    alert(getCss('.red', 'color'))
</script>
Web开发学习资料推荐
hongmei85 2018-02-02
  • 打赏
  • 举报
回复
[code=asp] <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> new document </title> <style>.datagrid-row-selected { background: #00bbee; color: #fff; }</style> <script type='text/javascript'> function GetCurrentStyle(obj, prop) { if (obj.currentStyle) { return obj.currentStyle[prop]; } else if (window.getComputedStyle) { propprop = prop.replace(/([A-Z])/g, "-$1"); propprop = prop.toLowerCase(); return document.defaultView.getComputedStyle(obj, null)[prop]; } return null; } </script> </head> <body ng-app="myApp" ng-controller="myCtrl"> <div onclick="alert(GetCurrentStyle(document.getElementById('dd'),'background-color'))" id="dd" class="datagrid-row-selected"> sdfsdf </div> </body> </html> [/code]
Go 旅城通票 2018-02-02
  • 打赏
  • 举报
回复
遍历样式表

<style>
    .blue{color: Blue;}
    .red{color:red}
</style>
<script>
    function getCss(theClass, attr) {
        var sl = document.styleSheets.length;
        if (sl == 0) return;
        var cssRules = document.styleSheets[0].rules ? 'rules' : 'cssRules';
        for (var i = 0; i < sl; i++) {
            for (var j = 0, rl = document.styleSheets[i][cssRules].length; j < rl; j++) {
                if (document.styleSheets[i][cssRules][j].selectorText == theClass) {
                    return document.styleSheets[i][cssRules][j].style[attr]
                }
            }
        }
    }
    alert(getCss('.blue', 'color'))
    alert(getCss('.red', 'color'))
</script>


Web开发学习资料推荐
aiit 2018-02-02
  • 打赏
  • 举报
回复
可能我没说明白,我的意思是能不能不通过实例obj(在您的例子里是<div>),而是通过函数来获得,比如说 getBackgroudColorByClass('datagrid-row-selected') 来得到背景色,先谢过了。
引用 1 楼 hongmei85 的回复:
[code=asp] <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> new document </title> <style>.datagrid-row-selected { background: #00bbee; color: #fff; }</style> <script type='text/javascript'> function GetCurrentStyle(obj, prop) { if (obj.currentStyle) { return obj.currentStyle[prop]; } else if (window.getComputedStyle) { propprop = prop.replace(/([A-Z])/g, "-$1"); propprop = prop.toLowerCase(); return document.defaultView.getComputedStyle(obj, null)[prop]; } return null; } </script> </head> <body ng-app="myApp" ng-controller="myCtrl"> <div onclick="alert(GetCurrentStyle(document.getElementById('dd'),'background-color'))" id="dd" class="datagrid-row-selected"> sdfsdf </div> </body> </html> [/code]
引用 1 楼 hongmei85 的回复:
[code=asp] <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> new document </title> <style>.datagrid-row-selected { background: #00bbee; color: #fff; }</style> <script type='text/javascript'> function GetCurrentStyle(obj, prop) { if (obj.currentStyle) { return obj.currentStyle[prop]; } else if (window.getComputedStyle) { propprop = prop.replace(/([A-Z])/g, "-$1"); propprop = prop.toLowerCase(); return document.defaultView.getComputedStyle(obj, null)[prop]; } return null; } </script> </head> <body ng-app="myApp" ng-controller="myCtrl"> <div onclick="alert(GetCurrentStyle(document.getElementById('dd'),'background-color'))" id="dd" class="datagrid-row-selected"> sdfsdf </div> </body> </html> [/code]
引用 1 楼 hongmei85 的回复:
[code=asp] <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> new document </title> <style>.datagrid-row-selected { background: #00bbee; color: #fff; }</style> <script type='text/javascript'> function GetCurrentStyle(obj, prop) { if (obj.currentStyle) { return obj.currentStyle[prop]; } else if (window.getComputedStyle) { propprop = prop.replace(/([A-Z])/g, "-$1"); propprop = prop.toLowerCase(); return document.defaultView.getComputedStyle(obj, null)[prop]; } return null; } </script> </head> <body ng-app="myApp" ng-controller="myCtrl"> <div onclick="alert(GetCurrentStyle(document.getElementById('dd'),'background-color'))" id="dd" class="datagrid-row-selected"> sdfsdf </div> </body> </html> [/code]
引用 1 楼 hongmei85 的回复:
[code=asp] <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> new document </title> <style>.datagrid-row-selected { background: #00bbee; color: #fff; }</style> <script type='text/javascript'> function GetCurrentStyle(obj, prop) { if (obj.currentStyle) { return obj.currentStyle[prop]; } else if (window.getComputedStyle) { propprop = prop.replace(/([A-Z])/g, "-$1"); propprop = prop.toLowerCase(); return document.defaultView.getComputedStyle(obj, null)[prop]; } return null; } </script> </head> <body ng-app="myApp" ng-controller="myCtrl"> <div onclick="alert(GetCurrentStyle(document.getElementById('dd'),'background-color'))" id="dd" class="datagrid-row-selected"> sdfsdf </div> </body> </html> [/code]

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧