Use a plain CSS to disable the a tag in HTML without JavaScript

Source: Internet
Author: User

In fact, this problem in the first time to learn HTML in the Select tag has come out, today, still do not find the use of pure CSS to disable a-label approach-colleagues, classmates, teachers I have asked, they are all the same with the use of JavaScript, Do you really have to use JavaScript?

1, pure CSS to implement the HTML in the A-label disable:

Copy CodeThe code is as follows:
<title> How to disable a label </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>
<body>
<aclass= "Disablecss" href= "http://www.baidu.com/" > Baidu </a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<aclass= "Disablecss" href= "#" onclick= "Javascript:alert (' Hello!!! ‘);" > Click </a>
</body>


Although the above uses pure CSS to implement the A-label disable, but because opera, IE browser does not support the pointer-events style, so the above code in these two types of browsers do not play the role of disabling a tag.

2. Use jquery and CSS to implement the disable of a tag in HTML:

Copy CodeThe code is as follows:
<title>02--disabling the A tag in HTML with jquery and CSS </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 ');//Remove the href attribute from the A tag
$ ('. Disablecss '). Removeattr (' onclick ');//Remove the OnClick event from the A-tag
});
</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>
<body>
<a class= "Disablecss" href= "http://www.baidu.com/" > Baidu </a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a class= "Disablecss" href= "#" onclick= "Javascript:alert (' Hello!!! ‘);" > Click </a>
</body>


This way can be compatible with all browsers, but mixed with JavaScript, this is probably fatal flaw!!!

3. Use jquery to implement the disable of a tag in HTML:

Copy CodeThe code is as follows:
<title>03--disabling the A tag in HTML with jquery </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 ');//Remove the href attribute from the A tag
$ ('. Disablecss '). Removeattr (' onclick ');//Remove the OnClick event from the A-tag
$ (". 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>
<body>
<a class= "Disablecss" href= "http://www.baidu.com/" > Baidu </a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a class= "Disablecss" href= "#" onclick= "Javascript:alert (' Hello!!! ‘);" > Click </a>
</body>


The above uses pure jquery to implement the ability to disable the a tag in HTML.

Use a plain CSS to disable the a tag in HTML without JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.