Skip to content

[html] 第89天 a标签的href和onclick属性同时存在时哪个先触发? #743

@haizhilin2013

Description

@haizhilin2013
Collaborator

第89天 a标签的href和onclick属性同时存在时哪个先触发?

Activity

xxf1996

xxf1996 commented on Jul 14, 2019

@xxf1996

应该是onclick属性先触发,判断依据是在onclik中使用preventDefault方法可以阻止a标签的跳转,说明a标签的跳转行为是一个默认行为;如下:

<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/a" onclick="event.preventDefault()">test</a>
xjt31012

xjt31012 commented on Jul 15, 2019

@xjt31012

测试了一下,是 href 里的先触发

haizhilin2013

haizhilin2013 commented on Jul 15, 2019

@haizhilin2013
CollaboratorAuthor

@xjt31012 为什么呢?

huangsw0411

huangsw0411 commented on Jul 15, 2019

@huangsw0411

先执行onclick,最后载入到href的链接。

kruzabc

kruzabc commented on Jan 8, 2020

@kruzabc
<a href="javascript:alert(1)" onclick="alert(2)">点一下试试,看谁先触发</a>

<a href="javascript:alert(1)" onclick="alert(2);return false;">点一下试试</a>

onclick事件先触发, 如果函数执行返回false(全等), 则href不会被触发。
只在chromefrrefox 中试过

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@xjt31012@xxf1996@huangsw0411@kruzabc

        Issue actions

          [html] 第89天 a标签的href和onclick属性同时存在时哪个先触发? · Issue #743 · haizlin/fe-interview