繁体   English   中英

未检测到addEventListerner

[英]addEventListerner not detected

我正在尝试构建一个可扩展的面板。 我希望有两个面板可以根据用户选择打开/关闭。 运行以下代码:

const classes = {
  isOpen: ".js-is-open"
};
const selectors = {
  panelTrigger: ".js-expandablePanel__title"
};
function expandablePanel() {
  document.querySelector(selectors.panelTrigger).addEventListener("click", () => { 
    console.log('dfgdfgdf');
    console.log(classes.isOpen);
  });
  console.log(selectors.panelTrigger);
  console.log(classes.isOpen);
}
export { expandablePanel };

出现以下错误:

ExpandablePanel.js:9 Uncaught TypeError: Cannot read property 'addEventListener' of null
at expandablePanel (ExpandablePanel.js:9)
at Object.defineProperty.value (ContentAuthoring.js:13)
at __webpack_require__ (bootstrap 29b82058c7ef95bf1e58:19)
at Object.<anonymous> (main.js:10)
at Object.defineProperty.value (main.bundle.js:53996)
at __webpack_require__ (bootstrap 29b82058c7ef95bf1e58:19)
at Object.defineProperty.value (index.js:12)
at __webpack_require__ (bootstrap 29b82058c7ef95bf1e58:19)
at Object.<anonymous> (formats.js:18)
at __webpack_require__ (bootstrap 29b82058c7ef95bf1e58:19)

我的HTML代码是:

<div class="js-expandablePanel__title">
    <p>
      Demo title 1
    </p>
  </div>

  <div class="js-expandablePanel">
   Demo panel 1
  </div>
   <div class="js-expandablePanel__title">
    <p>
      Demo title 1
    </p>
  </div>

  <div class="js-expandablePanel">
   Demo panel 1
  </div>

您能看到任何不喜欢我的addEventListener明显原因吗?

你需要一个 (。)

const selectors = {
  panelTrigger: ".js-expandablePanel__title"
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM