[02]a tag只为成button用时候设置href的办法

a tag为成button使用,
把JavaScript动作处理时,有如下四种停止Event效果。

  • <a href="#">
  • <a href="javascript:;">
  • <a href="javascript:void(0)">
  • <a role="button" tabindex="0">

按照如下顺序可以考虑使用。

  1. 可以用button tag的话,直接用Button Tag。(推荐)
  2. 如果需要用a tag : <a href="javascript:;" role="button">
    • script load前点击a tag也不会跟href="#"一样滚动条到页面上单。
    • javascript:void(0); 效果一样,但更简单
    • role: 可达性
    • tab focusing 可能 (通过用 href)
  3. 如果需要删除href property: <a role="button" tabindex="0"> (cursor 图表的话用 css 控制)
    • 在a tag上没有 href 的话不能Tab focusing,
    • 但设置tabindex="0" 的话会 focusable 。 (Tab顺序的话,按照基本Markup顺序做)
    • 参考: 如果禁止Focus的话用 `tabindex="-1"

参考 #

<a href="#"> 缺点 #

  • 不对hash的原来目的 (anchor 是移动特点位置是用的)
  • 点击的话会滚动条到页面上单。 (列: script load 前,script error时)

如果用javascript 处理tag的话,推荐使用button tag #

a tag只为成button使用时设置href办法 #

href="javascript:;" or href="javascript:void(0);"

  • 两个都一样。 (停止a tag的默认效果)
  • 用JavaScript点击处理时使用
  • script load前点击也不会跟href="#"一样滚动跳到页面上单

void(0) ? #

alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) // alerts "new value"
  • 为什么是0?: 很简单,很习用。
  • 很多minifiers把undefined换掉void 0 (用量小,安全)
  • 参考: 跟void 0一样的意思

<a role="button" tabindex="0"> 方式 #

http://wit.nts-corp.com/2014/04/14/1297 > "a tag"
https://stackoverflow.com/questions/10510191/valid-to-use-a-anchor-tag-without-href-attribute

需要使用 a tag + 删除href 的方式的话(没有目的地的话):

    1. 删除href property
    2. WAI-ARIA Spec上提供的role property里必要告诉是button
    3. 为了focusable设置tabindex="0"
      • Tab顺序的话,按照基本Markup顺序做
      • 没href property的话,不能Tab focus
原文地址:https://www.cnblogs.com/yeziTesting/p/7641422.html