How do I handle a click anywhere in the page, even when a certain element stops the propagation?

后端 未结 8 1812
臣服心动
臣服心动 2020-12-04 23:51

We are working on a JavaScript tool that has older code in it, so we cannot re-write the whole tool.

Now, a menu was added position fixed to the bottom and the clien

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 00:12

    If you make sure that this is the first event handler work, something like this might do the trick:

    $('*').click(function(event) {
        if (this === event.target) { // only fire this handler on the original element
            alert('clicked');
        }
    });
    

    Note that, if you have lots of elements in your page, this will be Really Very Slow, and it won't work for anything added dynamically.

提交回复
热议问题