我想知道是否可以更改HTML文本输入元素(文本框)中特定字符的背景颜色。 我正在编写一个javaScript(jQuery)验证器,用于检查转义字符数组。这是关于'模糊'功能的Jquery。 我的目标是改变用户在文本框本身内输入的非法字符的背景(从外部循环的开始向下6行)。我知道你可以改变整个文本框的背景颜色,但我只是想在文本框中定位非法字符。
这可能吗?谢谢,Alex
$('#contactInfoTable tr td input').on('blur', function() {
var illegalCharactersPattern = ["@","!","#","%","&",".","*","+","?","^","$","{","}","(",")","|","]","\",","[","/"];
var inputArray = this.value.split("");
var illegalCharactersEntered = [];
for(var i = 0; i < inputArray.length; i++) {
for(var j = 0; j < illegalCharactersPattern.length; j++){
if(inputArray[i] == illegalCharactersPattern[j]){
$(this.charAt(i)).css("backgroundColor", "yellow");
break;
} else {
this.style.backgroundColor = 'inherit';
}
}
}
} ) ;