为什么在尝试将元素追加到文档时出现错误?

问题描述 投票:1回答:1

我有自定义方法:

prependCities() {
  if(process.client) {
    var locations = document.createElement("datalist");
    locations.id = "locations";
    var cities = this.cities;
    for(var i = 0; i < cities.length; i++) {
        var opt = cities[i];
        var el = document.createElement("option");
        el.textContent = opt;
        el.value = opt;
        locations.appendChild(el);
    }​
    var body = document.getElementsByTagName("form")[0]; 
    body.insertBefore(locations, body.children[0]);
  }
}

当我在mounted钩子上运行此方法,然后收到错误消息:

SyntaxError:C:\ xampp \ htdocs \ newbabolo \ components \ MyForm.vue:意外字符''(420:9)

  418 |             el.value = opt;
  419 |             locations.appendChild(el);
> 420 |         }​
      |          ^
  421 |         var body = document.getElementsByTagName("form")[0]; 
  422 |         body.insertBefore(locations, body.children[0]);
  423 |       }

我有什么错误吗?

javascript vue.js vuejs2 nuxt.js nuxt
1个回答
0
投票

您在此列中有一个不可见的字符。

查看此处:https://jsfiddle.net/1tm5zqda/

prependCities() {
  if(process.client) {
    var locations = document.createElement("datalist");
    locations.id = "locations";
    var cities = this.cities;
    for(var i = 0; i < cities.length; i++) {
        var opt = cities[i];
        var el = document.createElement("option");
        el.textContent = opt;
        el.value = opt;
        locations.appendChild(el);
    }
    var body = document.getElementsByTagName("form")[0]; 
    body.insertBefore(locations, body.children[0]);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.