Jquery Selector that search for events

前端 未结 3 1510
暗喜
暗喜 2021-01-07 11:58

I need to select all elements, that has binded \"click\" event? Is there such selector exists?

相关标签:
3条回答
  • 2021-01-07 12:32

    It is not supported natively by jQuery, but you can write your own custom selector using hasEvent plugin :

    jQuery.expr[":"].click = "jQuery(a).hasEvent('click');";
    
    $("a:click").doStuff();
    

    EDIT :

    There is also the Event Bound Selector plugin which is more complete and works out of the box, but is also bigger.

    0 讨论(0)
  • 2021-01-07 12:35

    No. You can iterate over all elements and check if they have an event binding. But that wouldn't be very efficient unless you have a clue what kind of elements would have that event binding so you can narrow the search.

    0 讨论(0)
  • 2021-01-07 12:39

    Search for the position:

        button: function(elem){
            return "button" === elem.type || elem.nodeName.toLowerCase() === "button";
        },
        input: function(elem){
            return /input|select|textarea|button/i.test(elem.nodeName);
        },
        //init custom
        ev: function(elem,i,match){
            var what    = $(elem).hasEvent(match[3]);
            var type    = (typeof what);
            return  what !== null && type !== undefined;
        }
        //End custom
    },
    setFilters: {
        first: function(elem, i){
            return i === 0;
        },
        last: function(elem, i, match, array){
            return i === array.length - 1;
        },
        even: function(elem, i){
            return i % 2 === 0;
        },
    

    .....

    same use as has()

    ex:

    $('form:ev(submit)');
    $('input:ev(click)');
    $('a:ev(click)');
    
    0 讨论(0)
提交回复
热议问题