JavaScript modifies the class instance of an HTML element

Source: Internet
Author: User
The code is as follows Copy Code

<script type= "Text/javascript" >
function over () {
var para = document.getElementById ("Testpara"). ClassName = "Testover";
}
function out () {
var para = document.getElementById ("Testpara"). ClassName = "Testnormal";
}
</script>

HTML code

This example takes only one paragraph and sets the default class attribute "Testnormal" to it, and the mouse across and out will trigger the two JavaScript functions above, changing the class attribute of the paragraph itself.

The code is as follows Copy Code

<p id= "Testpara" class= "Testnormal" onmouseover= "Over ()" onmouseout= "Out ()" >
I am a sample paragraph, the mouse over me, you can change my class attributes, and thus change the application of CSS rules.
</p>


Css

The CSS code sets two test styles.

  code is as follows copy code

. testnormal {
 border:1px solid black;
 }
. testover {
 background: #999999;
 border:1px solid black;
 font-weight:bold;
 padding:3em;
 }

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.