JS獲得元素顏色


<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body{background: RGB(222,222,2);}
</style>
</head>

<body>
<input type='button' value='點擊按鈕獲取頁面背景色' onclick="fun()"/>


<script type="text/javascript">
/*
*javascript的style屬性只能獲取內聯樣式,對於外部樣式和嵌入式樣式*需要用currentStyle屬性。但這種方法有兼容性(FF和Chrome不支持)
*/
HTMLElement.prototype.__defineGetter__("currentStyle", function() {
return this.ownerDocument.defaultView.getComputedStyle(this, null);
});

function fun(){
var color = document.body.currentStyle.backgroundColor;
alert(color);
}
</script>
</body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM