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

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

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

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

    4 replies    2015-07-07 00:25:54 +08:00
    Septembers
        1
    Septembers  
       Jul 6, 2015
    lyhper
        2
    lyhper  
    OP
       Jul 6, 2015
    @Septembers 谢谢你,事件冒泡我知道,但是和这个好像没关系呀?比如点击一个checkbox,是先勾选它还是先执行事件处理程序?
    FuryBean
        3
    FuryBean  
       Jul 6, 2015   ❤️ 1
    先上规范:
    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
        4
    lyhper  
    OP
       Jul 7, 2015
    @FuryBean 十分感谢!我试了一下,同样存在这个问题的还有单选按钮
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   943 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 23:00 · PVG 07:00 · LAX 16:00 · JFK 19:00
    ♥ Do have faith in what you're doing.