时至今日,依然没有找到使用纯css禁用a标签的办法,难道真的必须要借助javascript吗?其实不然,方法有很多,下面为大家介绍下通过js、jquey以及css来实现禁用a标签
其实这个问题在初次学习html中select标签时就已经冒出来了,时至今日,依然没有找到使用纯css禁用a标签的办法——同事、同学、老师我都问过了,他们都千篇一律借助了javascript,难道真的必须要借助javascript吗?
1、纯css实现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="http://www.baidu.com/">百度</a>
<aclass="disablecss" href="#"onclick="javascript:alert('你好!!!');">点击</a>
</body>
</html>
上面虽然使用纯css实现了对a标签的禁用,不过由于opera、ie浏览器不支持pointer-events样式,所以上面代码在这两类浏览器中起不到禁用a标签的作用。
2、借助jquery和css实现html中a标签的禁用:
代码如下:
<html>
<head>
<title>02 ——借助jquery和css实现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 class="disablecss" href="http://www.baidu.com/">百度</a>
<a class="disablecss" href="#" onclick="javascript:alert('你好!!!');">点击</a>
</body>
</html>
这种方式可以兼容所有浏览器,可是混用了javascript,这一点恐怕是致命的缺憾!!!
3、借助jquery实现html中a标签的禁用:
代码如下:
<html>
<head>
<title>03 ——借助jquery实现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 class="disablecss" href="http://www.baidu.com/">百度</a>
<a class="disablecss" href="#" onclick="javascript:alert('你好!!!');">点击</a>
</body>
</html>
上面使用了纯jquery实现了禁用html中a标签的功能。
更多如何使用纯css禁用html中a标签无需javascript。
