JavaScript事件处理程序的分配方式如何影响其执行?

问题描述 投票:0回答:0

请考虑以下代码。

<!DOCTYPE html>
<title>Question</title>
<script>
    function report(handler, _this) {
        alert(
            "handler:\n" + handler + "\n" +
            "this: " + _this + "\n" +
            "event: " + event + "\n"
        )
    }
</script>
<input type=button id=A value=A onclick="report(A.onclick, this)">
<script>function f() {report(B.onclick, this)}</script>
<input type=button id=B value=B onclick="f()">
<input type=button id=C value=C>
<script>C.onclick = function () {report(C.onclick, this)}</script>

通过点击按钮,我已经看到。

  1. A.onclickB.onclick 用 "functiononclick(event) {...}"来包装。
  2. B.onclick, this 是窗口(而不是按钮)。

还有其他注意事项吗?

javascript eventhandler
© www.soinside.com 2019 - 2024. All rights reserved.