如何使用JavaScript比较分配给元素的颜色 [英] How to compare color assigned for an element using JavaScript
本文介绍了如何使用JavaScript比较分配给元素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用jQuery的JavaScript中,如何测试分配给元素的颜色是否为红色蓝色,且其id
为'ID'
.
In JavaScript using jQuery, how does one test whether the color assigned to an element is red blue, having an id
as 'ID'
.
用于设置元素颜色的JavaScript语句为:
The JavaScript statement used to set the color for the element is:
$('#ID').css({'background-color':'#FF0000'});
推荐答案
您应该能够使用仅带有一个参数的css
函数进行检查:
You should be able to check this with the css
function with only one argument:
if ($('#ID').css('background-color') == '#FF0000') {
// your code
}
我的偏好是添加一个类(red
或blue
)并使用hasClass
进行检查:
My preference would be to add a class (red
or blue
) and check using hasClass
:
if ($('#ID').hasClass('red')) {
// your code
}
这篇关于如何使用JavaScript比较分配给元素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文