开发者

How to compare color assigned for an element using JavaScript

开发者 https://www.devze.com 2023-02-01 11:22 出处:网络
In JavaScript using jQuery, how does one test whether the colo开发者_高级运维r assigned to an element is red blue, having an id as \'ID\'.

In JavaScript using jQuery, how does one test whether the colo开发者_高级运维r assigned to an element is red blue, having an id as 'ID'.

The JavaScript statement used to set the color for the element is:

$('#ID').css({'background-color':'#FF0000'});


You should be able to check this with the css function with only one argument:

if ($('#ID').css('background-color') == '#FF0000') {
    // your code
}

My preference would be to add a class (red or blue) and check using hasClass:

if ($('#ID').hasClass('red')) {
    // your code
}


I also would suggest using classes, but if you really want to know the color of something I would use a javascript library to parse the return from .css, like this one: http://www.phpied.com/rgb-color-parser-in-javascript/

Here is an example of it in use: http://jsfiddle.net/keegan3d/5SBpk/

0

精彩评论

暂无评论...
验证码 换一张
取 消