a标签⾥⾯设置onclick_详解a标签添加onclick事件的⼏种⽅式我们常⽤的在a中有点击事件:
1. a ="javascript:js_method();" rel="external nofollow"
这种⽅法在传递this等的时候很容易出问题,⽽且javascript:协议作为a的href属性的时候不仅会导致不 必要的触发
2. a href=":void(0);" rel="external nofollow" rel="external nofollow" οnclick="js_method()"
这种⽅法是很多⽹站最常⽤的⽅法,也是最周全的⽅法,onclick⽅法负责执⾏js函数,⽽void是⼀个操作符,void(0)返回undefined,地址不发⽣跳转。⽽且这种⽅法不会像第⼀种⽅法⼀样直接将js⽅法暴露在浏览器的状态栏。
3.a href="javascript:;" rel="external nofollow" rel="external nofollow" οnclick="js_method()"
这种⽅法跟跟2种类似,区别只是执⾏了⼀条空的js代码。
4.a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" οnclick="js_method()"
这种⽅法也是⽹上很常见的代码,#是标签内置的⼀个⽅法,代表top的作⽤。所以⽤这种⽅法点击后⽹页后返回到页⾯的最顶端。
5.a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" οnclick="js_method();return false;"
html href属性
这种⽅法点击执⾏了js函数后return false,页⾯不发⽣跳转,执⾏后还是在页⾯的当前位置。
综合上述,在a中调⽤js函数最适当的⽅法推荐使⽤:
a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" οnclick="js_method()"
a href="javascript:;" rel="external nofollow" rel="external nofollow" οnclick="js_method()"
a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" οnclick="js_method();return false;"
以上所述是⼩编给⼤家介绍的a标签添加onclick事件的⼏种⽅式详解整合,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对脚本之家⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。