无法使preventDefault起作用:item.preventDefault不是函数

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

我尝试创建一个js脚本,该脚本首先阻止默认设置,仅第二次放开链接以移动菜单,但是我无法使preventDefault正常工作,错误是:item.preventDefault不是一个函数。其他都可以,.setAttribute正常工作。

这是我没有运气尝试过的脚本:

var hasChild = document.querySelectorAll(".menu-item-has-children a");
hasChild.forEach(setPrevent);

function setPrevent(item) {
  item.setAttribute('data-active', 'false');
  item.addEventListener("click", respButAction);
  item.preventDefault();
  // this.preventDefault();
}

function respButAction(item) {
  isaActive = this.getAttribute('data-active');
  console.log(isaActive);

  if (isaActive == 'false') {
    this.setAttribute('data-active', 'true');
    // item.setAttribute('data-active', 'true');
    item.stopPropagation();
    // this.stopPropagation();
  }
  if (isaActive == 'true') {
    this.setAttribute('data-active', 'false');
    // item.setAttribute('data-active', 'false');
    item.preventDefault();
    //this.preventDefault();
  }
}
javascript preventdefault
1个回答
1
投票

正如我在评论中提到的。

© www.soinside.com 2019 - 2024. All rights reserved.