3 Star 54 Fork 14

Yaohaixiao / dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
matches.js 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
Yaohaixiao 提交于 2023-10-12 16:04 . chore: 调整单测代码
/**
* 获取 el 节点下匹配 selector 选择器的 DOM 节点
* ========================================================================
* Element.matches() 方法可以用来判断 DOM 元素是否与给定的选择器匹配,事件代理判断是
* 否触发绑定的代理事件回调函数,关键就是使用 Element.matches() 辨别当前事件触发的目
* 标 DOM 元素是否为事件代理所期望触发的目标。
* ========================================================================
* @method matches
* @see https://developer.mozilla.org/en-US/docs/web/api/element/matches
* @param {HTMLElement} el - (必须)DOM 元素
* @param {String} selector - (必须)匹配 DOM 元素的选择器
* @returns {HTMLElement|Boolean}
*/
const matches = (el, selector = '') => {
const sel = selector.replace(/^>/i, '')
if (!selector || !sel || !el) {
return false
}
/* istanbul ignore else */
if (el.matches) {
return el.matches(sel)
} else if (el.msMatchesSelector) {
return el.msMatchesSelector(sel)
} else {
return false
}
}
export default matches
JavaScript
1
https://gitee.com/yaohaixiao/dom.js.git
git@gitee.com:yaohaixiao/dom.js.git
yaohaixiao
dom.js
dom.js
main

搜索帮助