如何使用javascript自动完成搜索栏的把手?

x33g5p2x  于 2022-09-28  发布在  Java
关注(0)|答案(1)|浏览(155)

目前,我正在为ghost cms制作一个自动完成搜索栏,我必须使用手柄。我用html做过,这是错误的;那么,如何实现车把模板呢?这是html部分:

<div class="autocomplete" style="width:300px;">
   <input id="myInput" type="text" name="myCountry" placeholder="Search">
</div>

Javascript部分是:

<script>
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;
      a = document.createElement("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items");
      this.parentNode.appendChild(a);
      for (i = 0; i < arr.length; i++) {
        if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
          b = document.createElement("DIV");
          b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
          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) { 
        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;
    removeActive(x);
    if (currentFocus >= x.length) currentFocus = 0;
    if (currentFocus < 0) currentFocus = (x.length - 1);
    x[currentFocus].classList.add("autocomplete-active");
  }
  function removeActive(x) {
    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);
  });
}

var countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","];

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

所以,我的问题是,我是在一个.hbs文件中这样做的。我不知道如何使用车把搜索功能。如果我尝试创建

{{#each}} 
  {{somedata}}
{{/each}}

它不起作用。我应该如何将车把部分与javascript部分链接起来?我见过有人用jquery做这件事,但这件事让我头晕目眩。那么有谁能帮忙吗?附言:如果你无法理解,我也可以分享更多细节。

wnrlj8wa

wnrlj8wa1#

Handlebar文件由节点/express服务器渲染。您是否在发送结果之前使用了适当的路由处理程序来呈现hbs文件?
wrt Ghost CMS,这个hbs文件是部分还是全新的页面?
如果它是部分的,文件是否正确渲染?(可以在呈现页面后使用inspect元素来检查它)
如果它是一个全新的页面,那么路由中的路由配置是否正确。yaml文件?

相关问题