如何使用 JavaScript 获取任何元素的背景颜色,例如
<div>
?我试过:
<html>
<body>
<div id="myDivID" style="background-color: red">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<input type="button" value="click me" onclick="getColor();">
</body>
<script type="text/javascript">
function getColor() {
myDivObj = document.getElementById("myDivID")
if (myDivObj) {
console.log('myDivObj.bgColor: ' + myDivObj.bgColor); // shows: undefined
console.log('myDivObj.backgroundcolor: ' + myDivObj.backgroundcolor); // shows: undefined
//alert ( 'myDivObj.background-color: ' + myDivObj.background-color ); // this is not a valid property :)
console.log('style:bgColor: ' + getStyle(myDivObj, 'bgColor')); //shows: undefined
console.log('style:backgroundcolor: ' + getStyle(myDivObj, 'backgroundcolor')); // shows:undefined:
console.log('style:background-color: ' + getStyle(myDivObj, 'background-color')); // shows: undefined
} else {
console.error('Error: When function "getColor();" was called, no element existed with an ID of "myDivId".');
}
}
/* copied from `QuirksMode` - http://www.quirksmode.org/dom/getstyles.html - */
function getStyle(x, styleProp) {
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
return y;
}
</script>
</html>
获取号码:
window.getComputedStyle( *Element* , null).getPropertyValue( *CSS* );
例子:
window.getComputedStyle( document.body ,null).getPropertyValue('background-color');
window.getComputedStyle( document.body ,null).getPropertyValue('width');
~ document.body.clientWidth
与所有包含连字符的css属性一样,它们在JS中的对应名称是去掉连字符并将以下字母大写:
backgroundColor
alert(myDiv.style.backgroundColor);
简单的解决方案
myDivObj = document.getElementById("myDivID")
let myDivObjBgColor = window.getComputedStyle(myDivObj).backgroundColor;
现在背景颜色存储在新变量中。
使用 jQuery:
jQuery('#myDivID').css("background-color");
有原型:
$('myDivID').getStyle('backgroundColor');
使用纯 JS:
document.getElementById("myDivID").style.backgroundColor
这取决于您需要的 div 中的哪种样式。这是在
CSS
中定义的背景样式还是通过javascript(inline)
添加到当前节点的背景样式?
在
CSS
风格的情况下,你应该使用计算风格。就像你在 getStyle()
.
使用内联样式你应该使用
node.style
参考:x.style.backgroundColor
;
另请注意,您通过使用驼峰式/非连字符参考来选择样式,所以不是
background-color
,而是backgroundColor
;
这对我有用:
var backgroundColor = window.getComputedStyle ? window.getComputedStyle(myDiv, null).getPropertyValue("background-color") : myDiv.style.backgroundColor;
而且,甚至更好:
var getStyle = function(element, property) {
return window.getComputedStyle ? window.getComputedStyle(element, null).getPropertyValue(property) : element.style[property.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); })];
};
var backgroundColor = getStyle(myDiv, "background-color");
使用 JQuery:
var color = $('#myDivID').css("background-color");
您可以在您的控件上绘制一个画布元素,即使不显示它,如果它有多种颜色,也可以在您的控件中的特定位置获取颜色。
<head>
<style>
</style>
</head>
<body><div style="border:gray solid 2px;height:575px;width:700px;position:absolute;top:75px;right:15px;background:black;color:white;font-Family:Century SchoolBook;font-Size:18px;padding-Top:20px;" id="container101"><div id="header" style="text-Align:center">ColorPicker</div>
<div id="div101" style="position:absolute;top:60px;left:100px;width:500px;height:500px;"></div>
<canvas height="500" width="500" style="position:absolute;top:60px;left:100px;display:none;" id="canva"></canvas>
</div>
<script>
function create(r1,g1,b1,r2,g2,b2){dv101 =
document.querySelector("#div101");dv101.style.background = "linear-gradient(90deg, rgb("+r1+", "+g1+", "+b1+"), rgb("+r2+", "+g2+", "+b2+"))";
var can = document.querySelector("#canva");
var context = can.getContext("2d");
context.rect(0, 0, 500, 500);
var grd = context.createLinearGradient(0,0,can.width,0);
grd.addColorStop(0, "rgb("+r1+", "+g1+", "+b1+")");
grd.addColorStop(1, "rgb("+r2+", "+g2+", "+b2+")");
context.fillStyle = grd;
context.fillRect(0,0,500,500);
dv101.onmousemove = (event) => {
var posx = event.clientX-dv101.offsetLeft-
document.querySelector("#container101").offsetLeft;
var posy = event.clientY-dv101.offsetTop-
document.querySelector("#container101").offsetTop;
console.log(posx,posy);
var data = context.getImageData(posx,posy,1,1);
couleur = "rgb("+data.data[0]+","+data.data[1]+","+data.data[2]+")";
document.body.style.background=couleur;
} return couleur;
};create(255, 0, 0, 0, 0, 255);
var arr = new Array();
function inp(x,y){
var input1 = document.createElement('input');
var cont = document.querySelector("#container101");
cont.appendChild(input1);
arr.push(input1);
input1.type = "text";
input1.style = "outline:none;position:absolute;top:"+y+"px;left:"+x+"px;height:30px;width:60px;background:white;color:black;";
input1.value = 0;
input1.onkeydown = (event) => {
switch(event.keyCode){
case 38:
input1.value++;
create(arr[0].value, arr[1].value, arr[2].value, arr[3].value, arr[4].value,arr[5].value);
break;
case 40:
input1.value--;
create(arr[0].value, arr[1].value, arr[2].value, arr[3].value, arr[4].value,arr[5].value);
break;
};
}
};inp(5,60);inp(5,110);inp(5,160);inp(610,60);inp(610,110);inp(610,160);
</script>
</body>
</html>
这是一个工作小提琴。 https://jsfiddle.net/cdgpts9n/