이 둔화하는 것입니다. 그러나 "녹색", "빨간색"등의 단어를 자동 완성하려면이 작업을 수행해야합니다.
HMTL에는 입력과 div가 필요합니다. 입력은 입력 용이며 div는 제안을 표시합니다.
<input id="input" oninput="findSuggestions('input', 'suggestions')">
<div id="suggestions"></div>
그래서 입력하면 함수가 호출됩니다. 이 함수는 모든 제안이 포함 된 배열을 거칩니다.
var arySuggestions = ["Alarm", "Already" , "Ballon"] // This is where all you suggestions go
function findSuggestions(strInputId, strSuggestionsDivId) {
var objInput = document.getElementById(strInputId)
var strInput = objInput.value // get the current text
var objSuggestionsDiv = document.getElementById(strSuggestionsDivId)
if (strInput.length > 0) {
objSuggestionsDiv.innerHTML = ""; // empty the suggestion div, just in case
var objList = document.createElement("ul");
for (var i = 0; i < arySuggestions.length; i++) {
var word = arySuggestions[i]
var wordPart = word.substring(0,strInput.length)
if (word.length > strInput.length && wordPart === strInput) { // check if the words are matching
// if they do create a list entry
var objListEntity = document.createElement("li");
objListEntity.setAttribute("onclick", "complete('" + word + "', '" + strInputId + "', '" + strSuggestionsDivId + "');");
objListEntity.innerHTML = word;
objList.appendChild(objListEntity);
}
}
// show the suggestionList
objSuggestionsDiv.appendChild(objList);
} else {
objSuggestionsDiv.innerHTML = ""; // empty the suggestion div
}
}
두 번째 기능이 있습니다. 추천을 클릭하면 다음과 같이 채울 수 있습니다.
function complete(strComplete, strInputId, strSuggestionsDivId) {
document.getElementById(strInputId).value = strComplete;
document.getElementById(strSuggestionsDivId).innerHTML = ""; // empty the suggestion div
}
커서를 따라 가려면 원하는 CSS가 필요할 수 있습니다.
희망이 있습니다.
가능합니까? 예. 이 사이트에 대해 너무 광범위하게 질문하는 방법입니다 ... 또한 그렇습니다. * "autocomplete"* 또는 "typeahead"* – charlietfl
와 같은 용어로 타사 라이브러리를 사용하도록 허용 된 스크립트를 웹에서 검색하거나 직접해야합니까? –