首先是看下matches的用法
例子
html
<div class="a" data-id="1" onClick={this.getTarget.bind(this)}>
<div></div>
<p></p>
</div>
js
closest (el, selector) {
var matchesSelector = el.matches
while (el) {
if (matchesSelector.call(el, selector)) {
break
}
el = el.parentNode || el.parentElement
}
return el
}
getTarget (e) {
const target = e.target
const wrap = this.closest(target, '.movieList')
const id = wrap.getAttribute('data-id')
}
这时点击calss为a下的所有子元素,都可以获得date-id=1