Is right click a Javascript event?

后端 未结 18 2120
南笙
南笙 2020-11-22 10:02

Is right click a Javascript event? If so, how do I use it?

18条回答
  •  情深已故
    2020-11-22 10:35

    Try using the which and/or button property

    The demo

    function onClick(e) {
      if (e.which === 1 || e.button === 0) {
        console.log('Left mouse button at ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 2 || e.button === 1) {
        console.log('Middle mouse button ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 3 || e.button === 2) {
        console.log('Right mouse button ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 4 || e.button === 3) {
        console.log('Backward mouse button ' + e.clientX + 'x' + e.clientY);
      }
      if (e.which === 5 || e.button === 4) {
        console.log('Forward mouse button ' + e.clientX + 'x' + e.clientY);
      }
    }
    window.addEventListener("mousedown", onClick);
    
    

    Some info here

提交回复
热议问题