gpt4 book ai didi

javascript - 如何在 js 数组中搜索?

转载 作者:行者123 更新时间:2023-11-28 01:16:40 25 4
gpt4 key购买 nike

当用户在文本区域中输入 @ 时,我试图建议人名。这是我到目前为止所做的:

var Names = $('td').map(function() { return $(this).text(); }).get();

function SuggestPeople() {
var $textarea = $('.TxtArea');
var textarea = $textarea[0];
var sel = $textarea.getSelection();
var val = textarea.value;
var pos = sel.start;

if(val.substr(pos-1,1) == '@'){
for (var i = 0; i < Names.length; i++) {
Names[i] = "<span>" + Names[i] + "</span>";
}
$('.SuggestPeople').html(Names);
} else {
$('.SuggestPeople').html('');
}

}

$('.TxtArea').on('keydown click', function(e) {
// e.preventDefault();
SuggestPeople();
});
table{
display:none;
}

div{
background-color:#eee;
min-height: 20px;
width: 200px;
padding-top:5px;
margin-bottom: 3px;
}

div > span{
border: 1px solid gray;
margin: 0 3px;
}

textarea{
width: 195px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rangyinputs.googlecode.com/svn/trunk/rangyinputs_jquery.min.js"></script>

<table>
<tr>
<td>Jack</td>
<td>Peter</td>
<td>Barmar</td>
</tr>
</table>

<div class="SuggestPeople"></div>
<textarea class="TxtArea"></textarea>


在上面的代码中,它向用户建议所有名称​​(存在于数组中)。但实际上我需要改进它,只建议那些接近用户在 @ 之后编写的名称。我怎样才能做到这一点?我想要类似 stackoverflow 的东西。

最佳答案

这里有一个适合您的解决方案:

// polyfills for older browsers

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position){
position = position || 0;
return this.substr(position, searchString.length) === searchString;
};
}

// code

var Names = $('td').map(function () {
return $(this).text();
}).get();
function SuggestPeople(e) {
var val = e.target.value,
suggest = document.getElementById('SuggestPeople');
if (val.substr(- 2).startsWith('@')) {
suggest.innerHTML = '<span>' + Names.map(function (v) {
var end = val.substr(- 1).toLowerCase();
if (v.startsWith(end) || v.startsWith(end.toUpperCase())) {
return v;
}
}).filter(function (v) {
return v
}).join('</span><span>') + '</span>'
} else {
suggest.innerHTML = '';
}

}

$('.TxtArea').on('keyup', function (e) {
// e.preventDefault();
SuggestPeople(e);
});
table{
display:none;
}

div{
background-color:#eee;
min-height: 20px;
width: 200px;
padding-top:5px;
margin-bottom: 3px;
}

div > span{
border: 1px solid gray;
margin: 0 3px;
}

textarea{
width: 195px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rangyinputs.googlecode.com/svn/trunk/rangyinputs_jquery.min.js"></script>

<table>
<tr>
<td>Jack</td>
<td>Peter</td>
<td>Barmar</td>
</tr>
</table>

<div id="SuggestPeople"></div>
<textarea class="TxtArea"></textarea>

请注意 String.prototype.startsWith() 在 IE 和旧版浏览器中不起作用,但您可以在这里找到一个 polyfill:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

关于javascript - 如何在 js 数组中搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526143/

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