使用純css禁用html中a標簽無需JavaScript

字號:


    其實這個問題在初次學習html中select標簽時就已經(jīng)冒出來了,時至今日,依然沒有找到使用純css禁用a標簽的辦法——同事、同學、老師我都問過了,他們都千篇一律借助了JavaScript,難道真的必須要借助JavaScript嗎?
    1、純css實現(xiàn)html中a標簽的禁用:
    代碼如下:
    <html>
    <head>
    <title>如何禁用a標簽</title>
    <metacontent="text/html; charset=GB2312"http-equiv="Content-Type">
    <style type="text/css">
    body{
    font:12px/1.5 \5B8B\4F53, Georgia, Times New Roman, serif, arial;
    }
    a{
    text-decoration:none;
    outline:0 none;
    }
    .disableCss{
    pointer-events:none;
    color:#afafaf;
    cursor:default
    }
    </style>
    </head>
    <body>
    <aclass="disableCss" href="#"onclick="javascript:alert('你好!?。?);">點擊</a>
    </body>
    </html>
    上面雖然使用純css實現(xiàn)了對a標簽的禁用,不過由于opera、ie瀏覽器不支持pointer-events樣式,所以上面代碼在這兩類瀏覽器中起不到禁用a標簽的作用。
    2、借助Jquery和css實現(xiàn)html中a標簽的禁用:
    代碼如下:
    <html>
    <head>
    <title>02 ——借助Jquery和css實現(xiàn)html中a標簽的禁用</title>
    <meta content="text/html; charset=GB2312" http-equiv="Content-Type">
    <script type="text/javascript" src="./jquery-1.6.2.js"></script>
    <script type="text/javascript">
    $(function() {
    $('.disableCss').removeAttr('href');//去掉a標簽中的href屬性
    $('.disableCss').removeAttr('onclick');//去掉a標簽中的onclick事件
    });
    </script>
    <style type="text/css">
    body {
    font: 12px/1.5 \5B8B\4F53, Georgia, Times New Roman, serif, arial;
    }
    a {
    text-decoration: none;
    outline: 0 none;
    }
    .disableCss {
    color: #afafaf;
    cursor: default
    }
    </style>
    </head>
    <body>
    <a >百度</a>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="#" onclick="javascript:alert('你好?。。?);">點擊</a>
    </body>
    </html>
    這種方式可以兼容所有瀏覽器,可是混用了JavaScript,這一點恐怕是致命的缺憾?。?!
    3、借助Jquery實現(xiàn)html中a標簽的禁用:
    代碼如下:
    <html>
    <head>
    <title>03 ——借助Jquery實現(xiàn)html中a標簽的禁用</title>
    <meta content="text/html; charset=GB2312" http-equiv="Content-Type">
    <script type="text/javascript" src="./jquery-1.6.2.js"></script>
    <script type="text/javascript">
    $(function() {
    $('.disableCss').removeAttr('href');//去掉a標簽中的href屬性
    $('.disableCss').removeAttr('onclick');//去掉a標簽中的onclick事件
    $(".disableCss").css("font","12px/1.5 \\5B8B\\4F53, Georgia, Times New Roman, serif, arial");
    $(".disableCss").css("text-decoration","none");
    $(".disableCss").css("color","#afafaf");
    $(".disableCss").css("outline","0 none");
    $(".disableCss").css("cursor","default");
    });
    </script>
    </head>
    <body>
    <a >百度</a>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="#" onclick="javascript:alert('你好?。?!');">點擊</a>
    </body>
    </html>
    上面使用了純Jquery實現(xiàn)了禁用html中a標簽的功能。