Home >Web Front-end >JS Tutorial >A javascript script that can get the real background color of an element_javascript skills

A javascript script that can get the real background color of an element_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:11:111092browse


[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> function getBg(element) {//author: Longbill (www.longbill.cn) if (typeof element == "string") element = document.getElementById(element); if (!element) return; cssProperty = "backgroundColor"; mozillaEquivalentCSS = "background-color"; if (element.currentStyle) var actualColor = element.currentStyle[cssProperty]; else { var cs = document.defaultView.getComputedStyle(element, null); var actualColor = cs.getPropertyValue(mozillaEquivalentCSS); } if (actualColor == "transparent" && element.parentNode) return arguments.callee(element.parentNode); if (actualColor == null) return "#ffffff"; else return actualColor; } function go() { for(var i=1;i<=4;i++) eval("alert('div"+i+":'+getBg('div"+i+"'));"); } </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn