How to check String “green” is valid color or not in Jquery?

前端 未结 2 676
长情又很酷
长情又很酷 2021-01-18 23:17

I have scenario where user provide color name. For that I first need to check user provided color name is valid or not using jquery.

For example user can provide val

2条回答
  •  猫巷女王i
    2021-01-18 23:45

    You can set that color to any hidden test element(default color white) on page. then retrieve the color property from that element. if its other than white, then color is valid. else it is not.

    $('#testelement').css('background-color', colorstring);
    var coloris = !(/\d/.test(colorstring)) && (colorstring == "white" || $('#testelement').css('background-color') != "rgb(255, 255, 255)")   ? "valid" : "invalid";
    alert(colorstring +' is '+ coloris );
    

提交回复
热议问题