jQuery /deep/ delegation

jQuery /deep/ delegation

本文关键字:delegation deep jQuery      更新时间:2023-09-26
使用

webcomponents,您可以使用 /deep/ 来定位 shadowdom 的内部元素,在我尝试使用事件委托之前,它可以正常工作。

常规点击功能将起作用:

$('html /deep/ a').on('click', function(e) {
  e.preventDefault()
  console.log('foo')
})

委派似乎没有效果:

$('html').on('click', '/deep/ a', function(e) {
  e.preventDefault()
  console.log('bar')
})

知道在这里做什么吗?

Shadow DOM 重新定位事件,使它们看起来来自宿主元素。这是为了保留封装模型(否则您可以抓住目标并在组件内部四处乱窜)。

http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-301/#toc-events

也许您可以尝试委托给主机元素,然后使用event.path来确定是否单击了a