How to compare color assigned for an element using JavaScript

前端 未结 2 1678
挽巷
挽巷 2021-01-27 13:37

In JavaScript using jQuery, how does one test whether the color assigned to an element is red blue, having an id as \'ID\'.

The JavaScript stat

2条回答
  •  攒了一身酷
    2021-01-27 13:58

    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
    }
    

提交回复
热议问题