jQuery 如何使用jquery检查元素的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10411187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:17:28  来源:igfitidea点击:

How to check the background-color of an element using jquery

jquerycssbackground-color

提问by Ali Bassam

if($(this).css("background-color")==Crimson) 

is this correct ? .css("background-color","white") will change the color, but I don't want to change, I wanna know which color it is.

这样对吗 ?.css("background-color","white") 会改变颜色,但我不想改变,我想知道它是什么颜色。

回答by Muayyad Alsadi

it works like this

它是这样工作的

if ($("#notify-9").css('background-color')=="rgb(220, 20, 60)") alert("matched");

if ($("#notify-9").css('background-color')=="rgb(220, 20, 60)") alert("matched");

you need to convert name to red, green, blue components, you might use this tool

您需要将名称转换为红色、绿色、蓝色组件,您可以使用此工具

http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php

http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php

回答by Sarfraz

Use quotes ""or '':

使用引号""''

if($(this).css("background-color")=="Crimson") 

回答by Arvind Bhardwaj

Use quotes around the color name as:

在颜色名称周围使用引号:

if( $(this).css("background-color") == "Crimson" ) 

otherwise it is right.

否则是对的。

回答by Babita Singh Nair

just use below line

只需使用下面的行

if($(this).css("background-color")=="crimson") 

as css("background-color")attribute result will be in small letters . so if you will compare with capital obviously it will return false. :) Small trick hope that work

因为css("background-color")属性结果将以小写字母表示。因此,如果您将显然与资本进行比较,它将返回错误。:) 小技巧希望有用