1. <i id='2yEBa'><tr id='2yEBa'><dt id='2yEBa'><q id='2yEBa'><span id='2yEBa'><b id='2yEBa'><form id='2yEBa'><ins id='2yEBa'></ins><ul id='2yEBa'></ul><sub id='2yEBa'></sub></form><legend id='2yEBa'></legend><bdo id='2yEBa'><pre id='2yEBa'><center id='2yEBa'></center></pre></bdo></b><th id='2yEBa'></th></span></q></dt></tr></i><div id='2yEBa'><tfoot id='2yEBa'></tfoot><dl id='2yEBa'><fieldset id='2yEBa'></fieldset></dl></div>
    <legend id='2yEBa'><style id='2yEBa'><dir id='2yEBa'><q id='2yEBa'></q></dir></style></legend>
    • <bdo id='2yEBa'></bdo><ul id='2yEBa'></ul>
  2. <small id='2yEBa'></small><noframes id='2yEBa'>

    <tfoot id='2yEBa'></tfoot>

    1. Javascript 点击事件处理程序 - 我如何获得对点击项

      时间:2023-09-05

      1. <tfoot id='WQPoL'></tfoot>

        <small id='WQPoL'></small><noframes id='WQPoL'>

        <i id='WQPoL'><tr id='WQPoL'><dt id='WQPoL'><q id='WQPoL'><span id='WQPoL'><b id='WQPoL'><form id='WQPoL'><ins id='WQPoL'></ins><ul id='WQPoL'></ul><sub id='WQPoL'></sub></form><legend id='WQPoL'></legend><bdo id='WQPoL'><pre id='WQPoL'><center id='WQPoL'></center></pre></bdo></b><th id='WQPoL'></th></span></q></dt></tr></i><div id='WQPoL'><tfoot id='WQPoL'></tfoot><dl id='WQPoL'><fieldset id='WQPoL'></fieldset></dl></div>
            <tbody id='WQPoL'></tbody>

                <bdo id='WQPoL'></bdo><ul id='WQPoL'></ul>

              • <legend id='WQPoL'><style id='WQPoL'><dir id='WQPoL'><q id='WQPoL'></q></dir></style></legend>
              • 本文介绍了Javascript 点击事件处理程序 - 我如何获得对点击项目的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我的 HTML:

                <div id="x" onclick="clickHandler(event)">
                   <div id="button1">This turns green</div>
                   <div id="button2">This turns blue</div>
                </div>
                

                首先,为什么我应该将事件"传递给点击处理程序,而事件是某种系统关键字?另外,由于在容器 div 上标识了点击处理程序,我如何知道单击了哪个按钮?

                So first of all, why am I supposed to be passing "event" into the click handler and is event some kind of system keyword? Also, since the click handler is identified on the container div, how do I know which button has been clicked?

                推荐答案

                event 是一个 Event 对象,在触发事件时自动创建.请注意,您不必将其称为 event(我倾向于将其简称为 e).该 Event 对象具有许多描述它所代表的事件的属性.在这种情况下,您感兴趣的是 target,它显示了作为事件源的元素:

                event is an Event object which is created automatically when an event is fired. Note that you don't have to call it event (I tend to call it simply e). That Event object has a number of properties which describe the event it represents. In this case, the one you're interested in would be target, which shows the element that was the source of the event:

                function clickHandler(e) {
                    var target = e.target;
                }
                

                这是一个工作示例.

                不幸的是,事情从来没有这么简单.虽然规范说它应该是 event.target,但 Internet Explorer 喜欢与众不同,并选择使用 event.srcElement,因此您可能需要检查一下确保 event.target 存在!例如:

                Unfortunately, it's never quite that simple. While the specification says it should be event.target, Internet Explorer likes to be different, and chooses to use event.srcElement, so you probably want to put in a check to make sure event.target exists! For example:

                function clickHandler(e) {
                    var target = (e.target) ? e.target : e.srcElement;
                }
                

                这篇关于Javascript 点击事件处理程序 - 我如何获得对点击项目的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:触摸移动卡住 忽略取消触摸移动的尝试 下一篇:如何将多个外部侦听器注册到 d3 中的同一选择

                相关文章

                最新文章

                <tfoot id='c19Mb'></tfoot>

                  <small id='c19Mb'></small><noframes id='c19Mb'>

                    • <bdo id='c19Mb'></bdo><ul id='c19Mb'></ul>
                  1. <i id='c19Mb'><tr id='c19Mb'><dt id='c19Mb'><q id='c19Mb'><span id='c19Mb'><b id='c19Mb'><form id='c19Mb'><ins id='c19Mb'></ins><ul id='c19Mb'></ul><sub id='c19Mb'></sub></form><legend id='c19Mb'></legend><bdo id='c19Mb'><pre id='c19Mb'><center id='c19Mb'></center></pre></bdo></b><th id='c19Mb'></th></span></q></dt></tr></i><div id='c19Mb'><tfoot id='c19Mb'></tfoot><dl id='c19Mb'><fieldset id='c19Mb'></fieldset></dl></div>
                  2. <legend id='c19Mb'><style id='c19Mb'><dir id='c19Mb'><q id='c19Mb'></q></dir></style></legend>