美文网首页
matches:点击获取指定的target属性的运用

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

作者: jhs1873 | 来源:发表于2018-05-24 13:46 被阅读0次

首先是看下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

相关文章

网友评论

      本文标题:matches:点击获取指定的target属性的运用

      本文链接:https://www.haomeiwen.com/subject/jfcsjftx.html