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

前端 未结 5 882
闹比i
闹比i 2021-01-05 10:42
if($(this).css(\"background-color\")==Crimson) 

is this correct ? .css(\"background-color\",\"white\") will change the color, but I don\'t want t

相关标签:
5条回答
  • 2021-01-05 11:14

    Use quotes "" or '':

    if($(this).css("background-color")=="Crimson") 
    
    0 讨论(0)
  • 2021-01-05 11:15

    it works like this

    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

    0 讨论(0)
  • 2021-01-05 11:15

    Use quotes around the color name as:

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

    otherwise it is right.

    0 讨论(0)
  • 2021-01-05 11:31

    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

    0 讨论(0)
  • 2021-01-05 11:31

    Possible answers in JavaScript is like this:

    if(window.getComputedStyle(document.getElementById("notify-9"),null).getPropertyValue("background-color") == "rgb(220, 20, 60)"){
       alert("matched!");
    }
    

    // rgb(220, 20, 60) is equal to Crimson

    0 讨论(0)
提交回复
热议问题