cssmagic / action

Easy and lazy solution for click-event-binding.
95 stars 32 forks source link

在移动浏览器当中, 使用事件委托,绑定在 body 元素上,有的时候会冒泡不上去 #18

Closed litson closed 9 years ago

litson commented 9 years ago

在移动浏览器当中, 使用delegate(或on、live), 绑定在$wrapper(指父级元素), 有的时候会冒泡不上去,特别是层级复杂、伴有定位的时候

// 举个简单的例子;
// 但这个例子并不会发生上述问题,怪我比较罗嗦;

<body>
    <div id='view'>
        <a href='javascript:;' data-action='nothingToDo'>btn</a>
    </div>

</body>
// 直接绑定在body上,无效
$(document.body).on('click', '[data-action]' , function(){ 
    console.log(this) ;
});

// 绑定在较近的父级元素上,有效
$('#view').on('click', '[data-action]' , function(){ 
    console.log(this) ;
});

请问这个有没有比较好的解决方案;

cssmagic commented 9 years ago

我想我知道你描述问题是什么,请稍等,我会写篇文档来解答这个问题。

cssmagic commented 9 years ago

我猜测你遇到的情况跟层级和定位没有直接关系,跟目标元素的类型有关系。

详见这篇文档: #23。