推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
lyhper

js 事件处理函数和事件默认行为的执行先后问题

  •  1
     
  •   lyhper · Jul 6, 2015 · 4142 views
    This topic created in 4138 days ago, the information mentioned may be changed or developed.

    请问哪个先执行呢?如果是事件默认行为先执行,那为什么可以使用preventDefault(),returnValue=false等方式阻止默认行为呢?

    4 replies  •  2015-07-07 00:25:54 +08:00
    Septembers
    Septembers
    Jul 6, 2015
    1
    lyhper
    lyhper
    OP
    Jul 6, 2015
    2
    @Septembers 谢谢你,事件冒泡我知道,但是和这个好像没关系呀?比如点击一个checkbox,是先勾选它还是先执行事件处理程序?
    FuryBean
    FuryBean
    Jul 6, 2015❤️1
    3
    先上规范:
    https://w3c.github.io/uievents/#event-flow-default-cancel

    大部分情况下EventListener先执行,规范上有写:
    Default actions should be performed after the event dispatch has been completed, but in exceptional cases may also be performed immediately before the event is dispatched.

    接着举了一个例子,就是checkbox的例子:
    The default action associated with the click event on <input type="checkbox"> elements toggles the checked IDL attribute value of that element. If the click event's default action is cancelled, then the value is restored to its former state.
    lyhper
    lyhper
    OP
    Jul 7, 2015
    4
    @FuryBean 十分感谢!我试了一下,同样存在这个问题的还有单选按钮
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5498 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:03 · PVG 10:03 · LAX 18:03 · JFK 21:03
    ♥ Do have faith in what you're doing.