使用纯css禁用html中a标签无需JavaScript
2014-05-14来源:

1、纯css实现html中a标签的禁用:

代码如下:

如何禁用a标签

body{

font:12px/1.5\5B8B\4F53,Georgia,TimesNewRoman,serif,arial;

}

a{

text-decoration:none;

outline:0none;

}

.disableCss{

pointer-events:none;

color:#afafaf;

cursor:default

}

百度

       

点击

Baidu
map

上面虽然使用纯css实现了对a标签的禁用,不过由于opera、ie浏览器不支持pointer-events样式,所以上面代码在这两类浏览器中起不到禁用a标签的作用。

2、借助Jquery和css实现html中a标签的禁用:

代码如下:

02——借助Jquery和css实现html中a标签的禁用

$(function(){

$('.disableCss').removeAttr('href');//去掉a标签中的href属性

$('.disableCss').removeAttr('onclick');//去掉a标签中的onclick事件

});

body{

font:12px/1.5\5B8B\4F53,Georgia,TimesNewRoman,serif,arial;

}

a{

text-decoration:none;

outline:0none;

}

.disableCss{

color:#afafaf;

cursor:default

}

百度

       

点击

Baidu
map

这种方式可以兼容所有浏览器,可是混用了JavaScript,这一点恐怕是致命的缺憾!!!

3、借助Jquery实现html中a标签的禁用:

代码如下:

03——借助Jquery实现html中a标签的禁用

$(function(){

$('.disableCss').removeAttr('href');//去掉a标签中的href属性

$('.disableCss').removeAttr('onclick');//去掉a标签中的onclick事件

$(".disableCss").css("font","12px/1.5\\5B8B\\4F53,Georgia,TimesNewRoman,serif,arial");

$(".disableCss").css("text-decoration","none");

$(".disableCss").css("color","#afafaf");

$(".disableCss").css("outline","0none");

$(".disableCss").css("cursor","default");

});

百度

       

点击

Baidu
map

上面使用了纯Jquery实现了禁用html中a标签的功能。

更多信息请查看IT技术专栏

2026上岸·考公考编培训报班

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
推荐信息
Baidu
map