如何使用。
我正在使用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 console
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题
突变。addednodes
是nodelist
(节点的收集)。因此,显然classList
不存在。您的类
popup__wrapper
用scxdxcfd
的后缀。没有精确名称popup__wrapper
。的类
解决方案
如果您需要检查
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 annodeList
(collection of nodes). So obviouslyclassList
not exists in it.Your class
popUp__wrapper
is suffixed with a anscxdxcfd
. No class with exact namepopUp__wrapper
.Solution
If you need to check
popUp__wrapper
available inside the node, usequerySelector
instead ofmatches
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