如何使用。

发布于 2025-01-21 18:43:12 字数 644 浏览 0 评论 0原文

我正在使用MutationObserver,并且想在target.classname上检查类选择器,但我会遇到一个错误,

Cannot read properties of undefined (reading 'contains')

因此我查看了ClassList的原型,它具有.contains方法,我做了以下target.classlist.prototype.contains(“ oferlayWrapper”),我仍然会遇到相同的错误。

我不明白为什么要检查部分className,为什么会遇到此错误。

编辑:我添加了控制台的快照 ”在此处输入映像说明“

编辑:我为代码做了一个示例,这是我正在寻找包含popup__wrapper 被添加到DOM中。 (当我们按打开按钮时,错误将在控制台中)

I am using MutationObserver and want to check a class selector on target.className but I get an error

Cannot read properties of undefined (reading 'contains')

so I looked at classList's prototype and it has .contains method and I did the following target.classList.prototype.contains("OverlayWrapper") and I still get the same error.

I don't understand why I am getting this error as I am trying to check for a partial className .

Edit: I have added a snapshot of the consoleenter image description here

Edit: I have made an example for the code, it is the bare minimum where I am looking for a class that contains popUp__wrapper being added to the DOM. (an error will be in the console when we press the open button) jsfiddle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2025-01-28 18:43:12

问题

  • 突变。addednodes nodelist (节点的收集)。因此,显然classList不存在。

  • 您的类popup__wrapperscxdxcfd的后缀。没有精确名称popup__wrapper

    的类

解决方案

let wrapperEl = Array.from(mutation.addedNodes) // convert NodeList to array
      .filter(n => n.nodeType != Node.TEXT_NODE) // remove text nodes
      .find(n => n.matches('[class*=popUp__wrapper]')) // search for class

如果您需要检查popup__wrapper在节点内可用,请使用queryselector而不是匹配

demo < >

/ strong >参考

https://develovelveling.mozilla。 org/en-us/docs/web/api/element/matches

https://developer.mozilla.org/en-us/docs/web/api/text

Issues

  • mutation.addedNodes is an nodeList (collection of nodes). So obviously classList not exists in it.

  • Your class popUp__wrapper is suffixed with a an scxdxcfd. No class with exact name popUp__wrapper.

Solution

let wrapperEl = Array.from(mutation.addedNodes) // convert NodeList to array
      .filter(n => n.nodeType != Node.TEXT_NODE) // remove text nodes
      .find(n => n.matches('[class*=popUp__wrapper]')) // search for class

If you need to check popUp__wrapper available inside the node, use querySelector instead of matches

Demo

https://jsfiddle.net/aswinkumar863/qjeafxd8/20/

References

https://developer.mozilla.org/en-US/docs/Web/API/Element/matches

https://developer.mozilla.org/en-US/docs/Web/API/Text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文