首页 > 解决方案 > 为什么没有将类添加到加载的导航元素?

问题描述

this.loadTemplate = async(currentPage) => {
  let promise = new Promise((resolve) => {
    $(() => $("#nav").load("./nav.html"));
    $(() => $("#footer").load("./footer.html"));
    resolve("resolved");
  });

  let result = await promise;
  console.log(result);
  $(`#${currentPage}`).addClass("current-page");
};

以上是加载类并将其添加到 nav.html 文件的代码。这应该将元素更改为橙色。它没有。

任何帮助表示赞赏。

标签: javascripthtmlcss

解决方案


为什么不让 jQuery 完成这项工作或不使用 jQuery

let currentPage = "someId";
$("#nav").load("./nav.html", function() {
  $("#footer").load("./footer.html", function() {
    $("#" + currentPage).addClass("current-page");
  })
})

推荐阅读