zoukankan      html  css  js  c++  java
  • JS a标签加入单击事件屏蔽href跳转页面

    1、

    a href="JavaScript:js_method();"

    这种方法在传递this等参数的时候很容易出问题,

    而且javascript:协议作为a的href属性的时候不仅会导致不必要的触发window.onbeforeunload事件,

    在IE里面更会使gif动画图片停止播放。W3C标准不推荐在href里面执行javascript语句

    2、

    a href="javascript:void(0);" onclick="js_method()"

    onclick方法负责执行js函数,而void是一个操作符,void(0)返回undefined,

    地址不发生跳转。而且这种方法不会像第一种方法一样直接将js方法暴露在浏览器的状态栏。

    3、

    a href="javascript:;" onclick="js_method()"

    这种方法跟跟2种类似,区别只是执行了一条空的js代码。

    4、

    a href="#" onclick="js_method()"

    #是标签内置的一个方法,代表top的作用。所以用这种方法点击后网页后返回到页面的最顶端。

    5、

    a href="#" onclick="js_method();return false;"

    这种方法点击执行了js函数后return false,页面不发生跳转,执行后还是在页面的当前位置。

    推荐使用:

    a href="javascript:void(0);" onclick="js_method()"
    a href="javascript:;" onclick="js_method()"
    a href="#" onclick="js_method();return false;"
  • 相关阅读:
    POJ 2752 Seek the Name, Seek the Fame
    POJ 2406 Power Strings
    KMP 算法总结
    SGU 275 To xor or not to xor
    hihocoder 1196 高斯消元.二
    hihoCoder 1195 高斯消元.一
    UvaLive 5026 Building Roads
    HDU 2196 computer
    Notions of Flow Networks and Flows
    C/C++代码中的笔误
  • 原文地址:https://www.cnblogs.com/wongzzh/p/15094690.html
Copyright © 2011-2022 走看看