[英]jquery custom events confusion
I have read the custom events documentation of jQuery. 我已经阅读了jQuery的自定义事件文档。 Just when I started to understand that you can trigger those custom events yourself, and elements like a link or window own standard events like click, scroll or resize....
当我开始了解您可以自己触发这些自定义事件时,诸如链接或窗口之类的元素拥有诸如单击,滚动或调整大小之类的标准事件。
I was confused when I was reading the code from unveil.js At the bottom there is the following code: 当我从bread.js读取代码时,我很困惑。在底部有以下代码:
$w.on("scroll.unveil resize.unveil lookup.unveil", unveil);
What are those events doing and where are they set / triggered? 这些事件在做什么,它们在哪里设置/触发? And what is a "lookup" event at lookup.unveil.
在lookup.unveil中什么是“查找”事件。
Those are namespaced jQuery events 这些是命名空间的jQuery事件
In jQuery events can be namespaced to make them unique and easier to work with. 在jQuery中,可以对事件进行命名空间,以使其具有唯一性并易于使用。
Generally you'd add a click event like this 通常,您会添加这样的click事件
$('.element').on('click', fn);
but what if you wanted to remove the event from just one element and you did 但是如果您只想从一个元素中删除该事件并且您做了
$('.element').off('click', fn);
you'd remove the event from all elements, so you can namespace the event instead 您将从所有元素中删除事件,因此可以为事件命名空间
$('.element').on('click.custom', fn);
The names doesn't matter, it's still a click
event, but now it can be removed without affecting other elements matching the selector that also has a click
event by doing 名称无关紧要,它仍然是
click
事件,但是现在可以删除它,而不会影响与也具有click
事件的选择器匹配的其他元素
$('.element').off('click.custom', fn);
to trigger such an event, one would just do 触发这样的事件,
$('.element').trigger('click.custom');
and it would also be triggered by a regular click, just like any click handler. 就像任何点击处理程序一样,它也会由常规点击触发。
In jQuery one can also define custom events, which you probably know if you're read the Custom Events docs , and it's as easy as doing 在jQuery中,您还可以定义自定义事件,如果您正在阅读“ 自定义事件”文档 ,则可能会知道这些,就像这样做一样容易
$('.elements').on('lookup.unveil', fn);
where .unveil
is just the custom namespace for the plugin, and that event can be triggered at the appropriate time by doing 其中
.unveil
只是插件的自定义名称空间,可以通过执行以下操作在适当的时间触发该事件
$('.elements').trigger('lookup.unveil');
and as it's not a native event, it doesn't really get triggered by anything else. 而且由于它不是本地事件,因此它不会真正被其他任何事件触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.