这里是用文本输入框还有文本域绑定了失去焦点事件,然后再遍历敏感词数组进行匹配和替换。
- var keywords=["阿扁推翻","阿宾","阿賓"];
- $("input[type=text]").live("blur",function(){
- _filter_method($(this));
- });
- $("textarea").live("blur",function(){
- _filter_method($(this));
- });
-
- function _filter_method(obj){
-
- var value = $(obj).val();
-
- for(var i=0;i<keywords.length;i++){
-
- var reg = new RegExp(keywords[i],"g");
-
- if(value.indexOf(keywords[i])!=-1){
- var result = value.replace(reg,"**");
- value = result;
- $(obj).val(result);
- }
- }
- }