matches:点击获取指定的target属性的运用

2018-05-24  本文已影响0人  jhs1873

首先是看下matches的用法

Element.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

上一篇 下一篇

猜你喜欢

热点阅读