Best way to get the Original Target

后端 未结 5 545
孤街浪徒
孤街浪徒 2020-12-05 17:53

What\'s a jQuery like and/or best practices way of getting the original target of an event in jQuery (or in browser javascript in general).

I\'ve been using someth

5条回答
  •  眼角桃花
    2020-12-05 18:34

    In conjunction with How to detect a click outside an element? here is how you might trap a sub-widget with similar hide-when-clicked-outside functionality to prevent your own pop-over from hiding along with it; in this case, we are trapping the JQuery UI Datepicker pop-over widget:

    // not using jquery .one handler for this, so we can persist outside click later
    $('html').click(function(evt) {
        // just return if element has the datepicker as a parent
        if ($(evt.target).parents('#ui-datepicker-div').length>0) return;
    
        //toggle shut our widget pop-over
        $('#mywidget').toggle();
    
        // now, unbind this event since our widget is closed:
        $(this).unbind(evt);
    });
    

提交回复
热议问题