使用 HTML5 数据属性的 MooTools 事件委托
是否可以使用 MooTools 中的 HTML5 数据属性进行事件委托?
我的 HTML 结构是:
<div id="parent">
<div>not selectable</div>
<div data-selectable="true">selectable</div>
<div>not selectable either.</div>
<div data-selectable="true">also selectable</div>
</div>
我想设置
来监听具有 only 的子元素上的所有点击>data-selected
属性。如果我做错了什么,请告诉我:
事件被设置为:
$("parent").addEvent("click:relay([data-selectable])", function(event, el) {
alert(this.get('text'));
});
但是单击回调会在单击所有 div 时触发,而不仅仅是定义了数据可选择属性的 div。您可以在 http://jsfiddle.net/NUGD4/ 上查看此示例
解决方法是将其添加为一个 CSS 类,它与委托一起使用,但我更希望能够使用数据属性,因为它在整个应用程序中使用。
Is it possible to have event delegation using the HTML5 data attributes in MooTools?
The HTML structure I have is:
<div id="parent">
<div>not selectable</div>
<div data-selectable="true">selectable</div>
<div>not selectable either.</div>
<div data-selectable="true">also selectable</div>
</div>
And I want to setup <div id="parent">
to listen to all clicks only on child elements that have the data-selected
attribute.
Please let me know if I'm doing something wrong:
The events are being setup as:
$("parent").addEvent("click:relay([data-selectable])", function(event, el) {
alert(this.get('text'));
});
but the click callback is fired on clicking all div's, not just the ones with a data-selectable attribute defined. You can see this example on http://jsfiddle.net/NUGD4/
A workaround is to adding this as a CSS class, which works with delegation but I would prefer to be able to use data-attributes as it's used throughout the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以做的是将未来的选择器引擎(从 1.3 开始)与 1.2 版本一起使用,只需按照以下说明操作:gist.github.com/361474
What you can do is use the future selector engine (from 1.3) with the 1.2 release, just follow these instructions: gist.github.com/361474
Mootools 不接受属性名称中的“-”。我认为,这是错误。使用下疼痛:
Mootools does not accept "-" in attribute name. I consider, it's bug. Use undersore:
从 MooTools 1.3 开始,下面的代码可以正常工作,如 DEMO 所示
Starting with MooTools 1.3, the code below works just fine as seen in this DEMO