关于禁用html中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标签的功能。
【0分demo下载】
相关文章
- HTML <!--...--> 注释标签
- Java-jacob-文件转HTML
- 使用UIWebView中html标签显示富文本
- html内容滚动
- 【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)
- [HTML 5] Inserting DOM as String template insertAdjacentHTML
- [HTML 5] Inserting DOM element
- jenkins通过命令行方式执行jmeter脚本及查看html报告
- 使用ABAP正则表达式解析HTML标签
- 关于 Spartacus 开源 Storefront 在 PWA 模式下运行时的 index.html hash mismatch 问题
- HTML meta refresh 刷新与跳转(重定向)页面
- Atitit usrQBK13 html dsl 规范与解决方案
- 关于Vue 中v-html 失效的原因
- flutter html
- 每个程序员和设计师必做的10项运动(http://begeek.cn/post/8271.html?_biz=MjM5OTA1MDUyMA==&mid=407358558&idx=2&sn=b218)
- HTML/XML转义字符对照表(建议收藏)
- html+css+js实现文本编辑器
- 爬虫新宠requests_html 带你甄别2019虚假大学 #华为云·寻找黑马程序员#
- python爬虫模块之HTML下载模块
- HTML 中框架、层的运用
- 关于对最新HTML总结PPT讲稿的分享
- HTML—讲解分割线标签、文字居中、背景图片充满全图