Validating css color names

前端 未结 7 1998
孤城傲影
孤城傲影 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:45

    Short JavaScript version

    Inspired by Valen's answer:

    v=c=>((s=document.head.style).color=c,q=s.color,s.color='',!!q);
    // v('red') => true
    // v('reeeed') => false
    

    More readably:

    const validColor = c => {
      document.head.style.color = c;
      const q = document.head.style.color;
      document.head.style.color = '';
      return !!q
    };
    

提交回复
热议问题