使用純css禁用html中a標(biāo)簽無需JavaScript

字號(hào):


    其實(shí)這個(gè)問題在初次學(xué)習(xí)html中select標(biāo)簽時(shí)就已經(jīng)冒出來了,時(shí)至今日,依然沒有找到使用純css禁用a標(biāo)簽的辦法——同事、同學(xué)、老師我都問過了,他們都千篇一律借助了JavaScript,難道真的必須要借助JavaScript嗎?
    1、純css實(shí)現(xiàn)html中a標(biāo)簽的禁用:
    代碼如下:
    <html>
    <head>
    <title>如何禁用a標(biāo)簽</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('你好?。?!');">點(diǎn)擊</a>
    </body>
    </html>
    上面雖然使用純css實(shí)現(xiàn)了對(duì)a標(biāo)簽的禁用,不過由于opera、ie瀏覽器不支持pointer-events樣式,所以上面代碼在這兩類瀏覽器中起不到禁用a標(biāo)簽的作用。
    2、借助Jquery和css實(shí)現(xiàn)html中a標(biāo)簽的禁用:
    代碼如下:
    <html>
    <head>
    <title>02 ——借助Jquery和css實(shí)現(xiàn)html中a標(biāo)簽的禁用</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標(biāo)簽中的href屬性
    $('.disableCss').removeAttr('onclick');//去掉a標(biāo)簽中的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('你好?。?!');">點(diǎn)擊</a>
    </body>
    </html>
    這種方式可以兼容所有瀏覽器,可是混用了JavaScript,這一點(diǎn)恐怕是致命的缺憾?。?!
    3、借助Jquery實(shí)現(xiàn)html中a標(biāo)簽的禁用:
    代碼如下:
    <html>
    <head>
    <title>03 ——借助Jquery實(shí)現(xiàn)html中a標(biāo)簽的禁用</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標(biāo)簽中的href屬性
    $('.disableCss').removeAttr('onclick');//去掉a標(biāo)簽中的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('你好?。?!');">點(diǎn)擊</a>
    </body>
    </html>
    上面使用了純Jquery實(shí)現(xiàn)了禁用html中a標(biāo)簽的功能。