在.trigger中使用时,冒号在jQuery中意味着什么? [英] What does colon mean in jQuery when used inside of .trigger?

查看:103
本文介绍了在.trigger中使用时,冒号在jQuery中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 http://api.jquery.com/trigger/ ,示例做了不回答我的问题。我正在查看一些代码,并想知道这段代码正在做什么。

I looked at http://api.jquery.com/trigger/ and the examples did not answer my question. I am looking at some code and would like to know what this block of code is doing.

$(document).on('click', '#SubmitQuery', function(event) {
            event.preventDefault();
            $(document).trigger('filter:submit');
        });

具体来说,该触发器函数内部的冒号有什么作用?对于完整的上下文,这里是什么过滤器(我假设触发器函数内部的'过滤器'引用该过滤器对象):

Specifically, what does the colon inside of that trigger function do? For complete context, here is what filter is (I assume that the 'filter' inside of the trigger function refers to that filter object):

var filter = {
    init: function() {
        $(document).on('keypress', '#Filter', debounce(function(event) {
            if (event.keyCode == 13) {
                $(document).trigger('filter:text');
            }
        }, 300));

        $(document).on('click', '#ClearFilter', function(event) {
            event.preventDefault();
            $('#FilterText').val('');
            $('#FilterText').focus();
            $(document).trigger('filter:clear');
        });

        $(document).on('change', '.filterSection [type=checkbox]', function(event) {
            var group = $(this).parents('[data-filter-group]').attr('data-filter-group');
            var $checkboxes = $('[data-filter-group=' + group + '] [type=checkbox]');

            if ($checkboxes.length > 0) {
                if ($checkboxes.filter(':checked').length === 0) {
                    $(this).prop('checked', true);
                }
            }
        });

        $(document).on('click', '#SubmitQuery', function(event) {
            event.preventDefault();
            $(document).trigger('filter:submit');
        });

        $("#Filter").focus();
    }
};


推荐答案

冒号指定自定义事件,实质上是为事件创建名称空间您可以稍后调用而不覆盖默认事件或必须在同一事件上创建多个侦听器。

The colons specifies custom events, essentially creating namespaces for events you can call later without overriding default events or having to create multiple listeners on the same event.

您可以在此处找到更多信息: https://learn.jquery.com/events/introduction-to-custom-events/

You can find more information here: https://learn.jquery.com/events/introduction-to-custom-events/

这篇关于在.trigger中使用时,冒号在jQuery中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆