【发布时间】:2012-10-19 15:34:50
【问题描述】:
不关心旧的浏览器回退。另外,不能使用库。
我有一个事件对象。我正在通过matchesSelector针对css选择器测试event.target:
event['target'].matchesSelector('css selector here');
这很有效:
event['target']['parentElement'].matchesSelector('css selector here');
...和:
event['target']['parentElement']['parentElement'].matchesSelector('css selector here');
我正在寻找的是一些超出我理解的可能的对象方法,我可以使用它来一直检查每个 parentElement 是否匹配,而无需 for 循环。我的重点是效率。
谢谢!
【问题讨论】:
-
只是一个想法......也许将 event.target 的父母“扁平化”为某种可以查询的对象?
-
为什么要避免
for循环?while循环也可以工作,或者可能是递归,但我不确定你为什么要这样做。 -
@pimvdb 为了获得绝对的最佳效率,一次访问和匹配事件对象属性的“浏览器原生”方式
-
技术上,在节点列表上使用
document.querySelectorAll('...')然后indexOf不会每次都需要matchesSelector。但我怀疑它会更快。matchesSelector只在元素上定义,而不是节点列表,所以你不能完全避免某种循环。
标签: javascript events match