替代 window.event

Substitute for window.event

似乎window.event在移动设备上不起作用,但在电脑上使用就可以了。

谁能告诉我,有什么可以替代window.event,以便在移动设备上激活推荐吗?

来自mdn

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

请参阅 addEventListener,它期望第二个参数为:

listener
The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function.

以及the EventListener interface的描述:

Parameters

event
The DOM Event which was triggered.

所以,简而言之:

eventTarget.addEventListener("click", yourFunction);

function yourFunction(evt) {
    // use the local evt variable 
    // do not use the global, non-standard event variable
}

使用arguments[0]

示例片段:

ib {
  display: inline-block;
  padding: 0.618em;
  border-radius: 0.618em;
}
.emphasize {
  /* make it really obnoxious */
  font-size: 2em;
  border: 1px solid yellow;
  font-weight: bold;
}
<ib style="background:#f88;cursor:pointer"
    onclick="arguments[0].target.closest('ib').classList.toggle('emphasize')">
  <p>Two inner blocks:</p>
  <ib style="background:#8f8">
    <p>One inner block:</p>
    <ib style="background:#88f">
      <p>First paragraph</p>
      <p>Then 2nd paragraph</p>
    </ib>
  </ib>
</ib>