find.js 606 B

123456789101112131415161718192021222324252627
  1. /**
  2. * 查询父级节点是否包含指定 ClassName
  3. * @param event
  4. * @param hasClass
  5. * @param maxDepth - 最大查询层级
  6. */
  7. export function checkAncestorClass(event, hasClass, maxDepth = 3) {
  8. let target = event.target
  9. let depth = 0 // 添加一个深度计数器
  10. while (target && target.nodeType === Node.ELEMENT_NODE && depth < maxDepth) {
  11. if (target.className.indexOf(hasClass) !== -1) {
  12. return {
  13. status: true,
  14. target
  15. }
  16. }
  17. target = target.parentNode
  18. depth++ // 每次循环增加深度计数
  19. }
  20. return {
  21. status: false,
  22. target
  23. }
  24. }