我有用户提供颜色名称的情况。为此,我首先需要检查用户提供的颜色名称是否有效,或者不使用jquery。
例如,用户可以提供橙色,拼写错误的值。
如何检查。
任何帮助对我来说都是最好的。
最佳答案
您可以使用以下方法来验证CSS颜色
css('backgroundColor', 'white')
设置任何颜色backgroundColor
进行应用; $('#input').keyup(function() {
if (this.value.trim()) {
var d = $('<span/>')
.css('backgroundColor', 'white')
.css('backgroundColor', this.value);
$('#op').html(this.value + (d.css('backgroundColor') == '' && this.value + (d.css('backgroundColor') != 'white' && d.css('backgroundColor') != 'rgb(255, 255, 255)') || /^white$/i.test(this.value) ? ' is valid' : ' is not valid'));
} else
$('#op').empty();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="input">
<div id="op"></div>