disable all click events on page (javascript)

后端 未结 8 1820
孤城傲影
孤城傲影 2020-12-13 15:11

Whats the easiest way to temporarily disable all mouse click/drag etc events through javascript?

I thought I could do document.onclick = function() { return fa

8条回答
  •  爱一瞬间的悲伤
    2020-12-13 15:33

    window.addEventListener("click", (e) => {
      e.stopPropagation();
      e.stopImmediatePropagation();
      e.preventDefault();
    }, true)
    

    If we added a listener to document instead of window anyone can add a listener to window and it works. Because of document child of window and its events trigger always after window events.

    We use 3 method of Event object.

    stopPropagation for prevent all capturing and bubbling

    stopImmediatePropagation for prevent same listeners (e.g. another window click listeners)

    preventDefault for prevent all user agent event (e.g anchor href or form submit)

提交回复
热议问题