繁体   English   中英

Javascript / JQuery更改元素后如何获取其实际颜色/实际颜色

[英]Javascript/JQuery How to get the live/actual color of an element after it has been changed

我有一个包含许多行的表,每行都有一个唯一的ID。 当用户单击一行时,它将更改行的颜色,使其像被选中时一样。 原始行颜色为“ #EBEBEB”

document.getElementById("selector" + iSelected).style.backgroundColor = '#F1F7DB';

当javascript成功更改背景颜色后,如何使用javascript检索其新颜色的值,

alert(document.getElementById('selector' + iSelected).style.backgroundColor);

尽管它现在是另一种颜色,但仍显示其原始值。

尝试这样的事情:

 window.addEventListener("click",function(){ document.getElementById("bar").style.background="rgb(69,69,69)"; alert(document.getElementById("bar").style.background); }) 
 #bar{width:100px; height:100px; background:pink;} 
 <div id="bar"></div> 

这是更改颜色并显示颜色的jQuery实现。 不幸的是,由于CSS中颜色的处理方式,您将无法获得“灰色”。 但是,您可以使用所有不同的预定义颜色来创建关联的数组并进行查找(如果您希望我对此进行评论,请注意)。

$(document).click(function () {
    $("#bar").css("background","gray"); //changes the color to gray
    alert($("#bar").css("background-color")); //makes a popup that displays the color
});

暂无
暂无

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

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