我正在使用jQuery.textcomplete插件包含以下代码:
$('#textarea3').textcomplete([
{ // html
mentions: {
'test': '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="572332242317303a363e3b7934383a" rel="noreferrer noopener nofollow">[email protected]</a>',
'othertest': '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="19766d717c6b6d7c6a6d597e74787075377a7674" rel="noreferrer noopener nofollow">[email protected]</a>'
},
match: /\B@(\w*)$/,
search: function (term, callback) {
callback($.map(this.mentions, function (mention) {
return mention.indexOf(term) === 0 ? mention : null;
}));
},
index: 1,
replace: function (mention) {
return '@' + mention + ' ';
}
}
]);
我希望当用户输入@te时显示选项,但仅显示提及。[值]
并且我不想返回替换: 函数
类似
replace: function (mention) {
return '@' + mention.[key] + ' ';
}
那么,我该怎么办?
最佳答案
将提到的 dict 转换为数组即可。
var mentions = {
'test': '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed99889e99ad8a808c8481c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>',
'othertest': '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d6cdd1dccbcddccacdf9ded4d8d0d597dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>'
};
var arrayOfMentions = Object.keys(mentions).map(function(key) {
var val = {};
val[key] = mentions[key];
return val;
});
$('textarea').textcomplete([
{
mentions: arrayOfMentions,
match: /\B@(\w*)$/,
search: function (term, callback) {
callback($.map(this.mentions, function (mention) {
var value = mention[Object.keys(mention)[0]];
return value.indexOf(term) === 0 ? mention : null;
}));
},
template: function (mention) {
return mention[Object.keys(mention)[0]];
},
index: 1,
replace: function (mention) {
var key = Object.keys(mention)[0];
return '@' + key + ' ';
}
}
]);
关于javascript - 将 jQuery Text Complete 与包含键和值的数组一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105597/