jquery if background color ==

前端 未结 3 958
刺人心
刺人心 2020-12-11 06:54

I am trying to check for the background of an element, here is my code. But it doesn\'t work:

I tried two ways, here is the first:

function changeCol         


        
3条回答
  •  孤城傲影
    2020-12-11 07:10

    I'm not sure what field is referring to, but my guess stands that it is the id of the element?

    If so, try wrapping field like so $('#' + field).css.

    NOTE: Here's a VERY NICE function for converting HEX to RGB

    function hexToRgb(string)
    {
        if(!string || typeof string !== 'string') return false;
    
        if(
            string.substring(0,1) == '#' && 
            (string.length == 4 || string.length == 7) && 
            /^[0-9a-fA-F]+$/.test(string.substring(1, string.length))
        ){
        string = string.substring(1, string.length);
    
        if(string.length == 3) 
            string = string[0] + string[0] + string[1] + string[1] + string[2] + string[2];
    
        return 'rgb(' + 
            parseInt(string[0] + string[1], 16).toString() + ',' + 
            parseInt(string[2] + string[3], 16).toString() + ',' + 
            parseInt(string[4] + string[5], 16).toString() + 
        ')';
    }
    else return false;
    }
    

提交回复
热议问题