如何创建自动完成

学习如何创建自动完成功能。

自动完成

请开始输入:

亲自试一试

创建自动完成表单

第一步 - 添加 HTML:


第二步 - 创建 JavaScript 数组:

包含世界上所有国家/地区的数组:

var countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central Arfrican Republic","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauro",နိုပါဒေးလီး နိုင်ငံများ အခြေအနေများ "Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"];

တတိယ အဆင့် - စက်တင် CSS:

အကွဲအဝေး အခြေခံ သည် 'အကွဲအဝေး' ဖြစ်စေရမည်。

* { box-sizing: border-box; }
body {
  font: 16px Arial;
}
.autocomplete {
  /* အကွဲအဝေး အခြေခံ ဖြစ်စေရမည်: */
  position: relative;
  display: inline-block;
}
input {
  border: 1px solid transparent;
  background-color: #f1f1f1;
  padding: 10px;
  font-size: 16px;
}
input[type=text] {
  background-color: #f1f1f1;
  width: 100%;
}
input[type=submit] {
  background-color: DodgerBlue;
  color: #fff;
}
.autocomplete-items {
  position: absolute;
  border: 1px solid #d4d4d4;
  border-bottom: none;
  border-top: none;
  z-index: 99;
  /* အကယ်၍ လုပ်ဆောင်ရေး အရာကို အကွဲအဝေး နှင့် အတူ တည်ဆောက်ရမည်: */
  top: 100%;
  left: 0;
  right: 0;
}
.autocomplete-items div {
  padding: 10px;
  cursor: pointer;
  background-color: #fff;
  border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
  /* 当鼠标悬停在项目上时: */
  background-color: #e9e9e9;
}
.autocomplete-active {
  /* 当使用箭头键浏览项目时:*/
  background-color: DodgerBlue !important;
  color: #ffffff;
}

第四步 - 添加 JavaScript:

function autocomplete(inp, arr) {
  /* 自动完成函数需要两个参数,一个是文本字段元素,另一个是可以自动完成的可能值的数组: */
  var currentFocus;
  /* 当有人在文本字段中写入时执行的函数: */
  inp.addEventListener("input", function(e) {
      var a, b, i, val = this.value;
      /* 关闭任何已经打开的自动完成值列表 */
      closeAllLists();
      if (!val) { return false;}
      currentFocus = -1;
      /* 创建一个包含项目(值)的 DIV 元素: */
      a = document.createElement("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items");
      /* 将这个 DIV 元素附加为自动完成容器的子元素:*/
      this.parentNode.appendChild(a);
      /* 遍历数组中的每个项目... */
      for (i = 0; i < arr.length; i++) {
        /* စာကြောင်းပိုင်းအား အော်ဂန်ဒီယိုအသုံးပြု၍ စတင်သည့် စာပုဒ်ကို စာကြောင်းပိုင်း၏ အသုံးပြုသည့် အသုံးပြုသည့် အရေးယူခြင်းသို့ ကူးယူသည်: */
        if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
          /* 为每个匹配元素创建一个 DIV 元素: */
          b = document.createElement("DIV");
          /* 将匹配的字母加粗: */
          b.innerHTML = "" + arr[i].substr(0, val.length) + "";
          b.innerHTML += arr[i].substr(val.length);
          /* 插入一个输入字段来保存当前数组项的值: */
          b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
          /* အတိုက်ရိုက် အမှတ် ပြောင်းလဲခြင်း အမှတ် ပြောင်းလဲခြင်း ကို အသိအမှတ်ပြုခြင်း ဖြစ်ပါတ် ကို လုပ်ဆောင်ရမည့် လုပ်ငန်း */
              b.addEventListener("click", function(e) {
              /* အတိုက်ရိုက် အမှတ် ပြောင်းလဲခြင်း အမှတ် ပြောင်းလဲခြင်း ကို ပြည့်ပြည်းအောင် ပေါင်းစပ်ခြင်း */
              inp.value = this.getElementsByTagName("input")[0].value;
              /* အတိုက်ရိုက် အမှတ် ပြောင်းလဲခြင်း ကို ပိတ်ခြင်း သို့မဟုတ် အခြား အတိုက်ရိုက် အမှတ် ပြောင်းလဲခြင်း ကို ပိတ်ခြင်း */
              closeAllLists();
          });
          a.appendChild(b);
        }
      }
  });
  /* ကိုယ်ငယ် အင်္ဂါလိုက် နှင့် ပေါင်းစပ်ခြင်း ဖြစ်ပါတ် ကို အခြေခံပြီး လုပ်ဆောင်ရမည့် လုပ်ငန်း */
  inp.addEventListener("keydown", function(e) {
      var x = document.getElementById(this.id + "autocomplete-list");
      if (x) x = x.getElementsByTagName("div");
      if (e.keyCode == 40) {
        /* အင်္ဂါလိုက် အမှတ် ၄၀ ခွဲ နှင့် ပေါင်းစပ်ခြင်း မပြုရပါ */
        currentFocus++;
        /* ဖြစ်ပါတ် ကို ပိုမို အသားတင်စေခြင်း */
        addActive(x);
      } else if (e.keyCode == 38) { //up
        /* အင်္ဂါလိုက် အမှတ် ၄၈ ခွဲ နှင့် ပေါင်းစပ်ခြင်း မပြုရပါ */
        currentFocus--;
        /* ဖြစ်ပါတ် ကို ပိုမို အသားတင်စေခြင်း */
        addActive(x);
      } else if (e.keyCode == 13) {
        /* အင်္ဂါလိုက် အက်စ် ခွဲ နှင့် ပေါင်းစပ်ခြင်း မပြုရပါ */
        e.preventDefault();
        if (currentFocus > -1) {
          /* 并模拟点击“活动”项目:*/
          if (x) x[currentFocus].click();
        }
      }
  });
  function addActive(x) {
    /* 将项目分类为“活动”的函数:*/
    if (!x) return false;
    /* 首先删除所有项目中的 "active" 类:*/
    removeActive(x);
    if (currentFocus >= x.length) currentFocus = 0;
    if (currentFocus < 0) currentFocus = (x.length - 1);
    /* 添加 "autocomplete-active" 类:*/
    x[currentFocus].classList.add("autocomplete-active");
  }
  function removeActive(x) {
    /* 从所有自动完成项中删除 "active" 类的函数:*/
    for (var i = 0; i < x.length; i++) {
      x[i].classList.remove("autocomplete-active");
    }
  }
  function closeAllLists(elmnt) {
    /*关闭文档中的所有自动完成列表,除了作为参数传递的那个:*/
    var x = document.getElementsByClassName("autocomplete-items");
    for (var i = 0; i < x.length; i++) {
      if (elmnt != x[i] && elmnt != inp) {
      x[i].parentNode.removeChild(x[i]);
    }
  }
}
/* 当有人单击文档时执行的函数*/
document.addEventListener("click", function (e) {
    closeAllLists(e.target);
});
}

ပုဂ္ဂိုလ်၏ နားနေရာ - "myInput" ပေါ်တွင် အကယ်၍ ပေးသွင်း စက်တင်ခြင်း ဖွင့်ပေးပါ:

နိုင်ငံ စကာတင်း အတွက် autocomplete ပုဂ္ဂိုလ်၏ ဒုတိယ ပြောင်းလဲခြင်းကို သယ်ပို့ပါ:

<script>
autocomplete(document.getElementById("myInput"), countries);

亲自试一试