简体   繁体   English

从一个对象中选择颜色,然后单击另一个对象并更改其背景颜色

[英]select colour from one object and then click on another object and change its background colour

Im making a game but I'm currently stuck on a problem that I've been trying to solve for a while now. 我正在制作游戏,但目前我陷入了一个已经试图解决了一段时间的问题。 I need to select a circle which is blue and then click on a clear circle and that clear circle needs to then change its attribute background to the blue but only when i click on the clear circle. 我需要选择一个蓝色的圆圈,然后单击一个透明的圆圈,然后该透明的圆圈需要将其属性背景更改为蓝色,但是只有当我单击该透明的圆圈时才可以。

supposing the first circle has id "circle-1" and the second "circle-2", and supposing you specify the color of your elements using the css property "background-color", you can do the following: 假设第一个圆圈的ID为“ circle-1”,第二个圆圈的ID为“ circle-2”,并且假设您使用css属性“ background-color”指定元素的颜色,则可以执行以下操作:

document.querySelector('#circle-2').style.backgroundColor = window.getComputedStyle(document.querySelector('#circle-1')).getPropertyValue('background-color');

Of course you have to bind this code on click of the circle and adapt it using $(this) to select the current color in order to make it dynamic 当然,您必须在单击圆圈时绑定此代码,并使用$(this)对其进行调整以选择当前颜色以使其动态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM