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

后端 未结 8 1796
臣服心动
臣服心动 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:36

    this is the key (vs evt.target). See example.

    document.body.addEventListener("click", function (evt) {
        console.dir(this);
        //note evt.target can be a nested element, not the body element, resulting in misfires
        console.log(evt.target);
        alert("body clicked");
    });

    This is a heading.

    this is a paragraph.

提交回复
热议问题