V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
lyhper
V2EX  ›  JavaScript

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

  •  1
     
  •   lyhper · 2015-07-06 22:33:26 +08:00 · 3222 次点击
    这是一个创建于 3224 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    4 条回复    2015-07-07 00:25:54 +08:00
    Septembers
        1
    Septembers  
       2015-07-06 22:54:31 +08:00
    lyhper
        2
    lyhper  
    OP
       2015-07-06 23:08:04 +08:00
    @Septembers 谢谢你,事件冒泡我知道,但是和这个好像没关系呀?比如点击一个checkbox,是先勾选它还是先执行事件处理程序?
    FuryBean
        3
    FuryBean  
       2015-07-06 23:33:47 +08:00   ❤️ 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
       2015-07-07 00:25:54 +08:00
    @FuryBean 十分感谢!我试了一下,同样存在这个问题的还有单选按钮
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1867 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:12 · PVG 00:12 · LAX 09:12 · JFK 12:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.