开发者

simple pro question: bind function again to an event - from the function ... kind of recoursive

开发者 https://www.devze.com 2023-02-24 20:59 出处:网络
if the user clicked on the certain target, then nothing should be done, but the click event should be bound \"one\" again - kind of recoursive ... how is this done nicely.

if the user clicked on the certain target, then nothing should be done, but the click event should be bound "one" again - kind of recoursive ... how is this done nicely.

        $(document).one('click',function(ev){
            if(!$(ev.target) == $('.gallerystatus input')){
                $('.galleryst开发者_JAVA技巧atus input').trigger('focusout');
            }
        });

Thanks for any advice!


I think this is what you're asking for:

    var clickGalleryStatus = function(ev){
        if(!$(ev.target) == $('.gallerystatus input')){
            $('.gallerystatus input').trigger('focusout');
        } else {
            $(document).one('click', clickGalleryStatus);
        }
    };

    $(document).one('click', clickGalleryStatus);

If .gallerystatus input isn't what's clicked, then it will rebind the click event.


Rather than rebinding the event if a condition does not occur it is better to unbind the event if it does:

var handler = function(ev){
    if(!$(ev.target) == $('.gallerystatus input')){
        $('.gallerystatus input').trigger('focusout');
        $(document).unbind('click', handler);
    }
};

$(document).bind('click', handler);
0

精彩评论

暂无评论...
验证码 换一张
取 消