Validating css color names

前端 未结 7 1972
孤城傲影
孤城傲影 2020-12-09 16:15

I\'ve written a jQuery plugin that accepts css colors for some of its parameters.

I want to validate them. If it was just a hex or rgb value I could do that with a r

7条回答
  •  囚心锁ツ
    2020-12-09 16:38

    You could simply set the color to a dummy element and then check if the element's value is something other than white.

    colorToTest = 'lime'; // 'lightgray' does not work for IE
    
    $('#dummy').css('backgroundColor', 'white');
    $('#dummy').css('backgroundColor', colorToTest);
    if ($('#dummy').css('backgroundColor') != 'rgb(255, 255, 255)' || colorToTest == 'white') {
        alert(colorToTest+' is valid');
    }
    

提交回复
热议问题