gpt4 book ai didi

javascript - Typeahead 不敏感的重音

转载 作者:搜寻专家 更新时间:2023-11-01 05:23:16 25 4
gpt4 key购买 nike

我试过了 this solution但我得到了这个错误:

Uncaught ReferenceError: normalized is not defined

这是我的代码:

var charMap = {
"à": "a", "â": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
"ï": "i", "î": "i", "ô": "o", "ö": "o", "û": "u", "ù": "u"
};

var normalize = function(str) {
$.each(charMap, function(chars, normalized) {
var regex = new RegExp('[' + chars + ']', 'gi');
str = str.replace(regex, normalized);
});
return normalized;
}

var queryTokenizer = function(q) {
var normalized = normalize(q);
return Bloodhound.tokenizers.whitespace(normalized);
};

var spectacles = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: queryTokenizer,
prefetch:'spectacles.json',
limit:10,
});
spectacles.initialize();

$('#search').typeahead({
minLength: 1,
hint:false,
highlight: true
},
{
name: 'spectacles',
displayKey: 'value',
source: spectacles.ttAdapter()
}) ;

我的错误在哪里?谢谢

最佳答案

如果您不想使用 Bloodhound,您可以从 Typeahead 对象自定义“highlighter”和“matcher”方法,使它们变得不区分重音。

如果你想让 typeahead 的默认行为不区分重音,你可以包含一个新的 JavaScript 文件,如下所示:

第 1 步 - 创建一个新文件 bootstrap3-typeahead-ci.min.js

// function for making a string accent insensitive
$.fn.typeahead.Constructor.prototype.normalize = function (str) {
// escape chars
var normalized = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

// map equivalent chars
normalized = normalized.replace(/[aãáàâ]/gi, '[aãáàâ]');
normalized = normalized.replace(/[eẽéèê]/gi, '[eẽéèê]');
normalized = normalized.replace(/[iĩíìî]/gi, '[iĩíìî]');
normalized = normalized.replace(/[oõóòô]/gi, '[oõóòô]');
normalized = normalized.replace(/[uũúùû]/gi, '[uũúùû]');
normalized = normalized.replace(/[cç]/gi, '[cç]');

// convert string to a regular expression
// with case insensitive mode
normalized = new RegExp(normalized, 'gi');

// return regular expresion
return normalized;
}

// change 'matcher' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.matcher = function (item) {

// get item to be evaluated
var source = this.displayText(item);

// make search value case insensitive
var normalized = this.normalize(this.query);

// search for normalized value
return source.match(normalized);
}

// change 'highlighter' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.highlighter = function (item) {

// get html output
var source = this.displayText(item);

// make search value case insensitive
var normalized = this.normalize(this.query);

// highlight normalized value in bold
return source.replace(normalized, '<strong>$&</strong>');
}

第 2 步 - 在 bootstrap3-typeahead.min.js 之后添加到您的页面

<script src="bootstrap3-typeahead.min.js"></script>
<script src="bootstrap3-typeahead-ci.min.js"></script>

当然,您必须意识到,由于您要替换这两种方法,因此您应该监控 typeahead 中发布的新功能是否不会反射(reflect)在您的自定义方法中。不过,我认为这是一个轻量级且快速的解决方案。

PS.:这是我对 Stack Overflow 的第一个贡献,希望您喜欢!

关于javascript - Typeahead 不敏感的重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com