Javascript click to change the CSS style

Source: Internet
Author: User

Javascript indirectly changes the CSS style by changing the element Class Name

CSS:

/* Font link style */TD. firstlevelmenuclass A: link {color: # 3e8bac; text-Decoration: none;}/* unaccessed link */TD. firstlevelmenuclass A: visited {color: # ffffff; text-Decoration: none;}/* accessed link */TD. firstlevelmenuclass A: hover {color: # ffffff;}/* move the cursor over the link */TD. firstlevelmenuclass A: active {color: # ffffff;}/* selected link * // * mouse event background style */TD. firstlevelmenuclass: hover {background-image: URL (.. /.. /public/img/menu_first_down_bg.gif);}/* move the cursor over the link */TD. firstlevelmenuclasshover {background-image: URL (.. /.. /public/img/menu_first_down_bg.gif);} TD. firstlevelmenuclass {background-image: URL (.. /.. /public/img/head_menu_center.gif );}

JS:

/** Obtain the HTML element of the corresponding class and tag * clsname: given class name * tagname: Given HTML element, if any tagname = '*' **/function getelementsbyclassname (clsname, tagname) {var classelements = []; selelements = document. getelementsbytagname (tagname); For (VAR I = 0; I <selelements. length; I ++) {If (selelements [I]. classname = clsname) {classelements [classelements. length] = selelements [I];} return classelements;} // indirectly change the background style by changing the element class name. Function onfirstmenuchangebg (E) {// first clear the changed element background style var getelements = getelementsbyclassname ('firstlevelmenuclasshover ', 'td'); For (VAR I = 0; I <getelements. length; I ++) {getelements [I]. classname = "firstlevelmenuclass";} // set the background style of the clicked element. classname = "firstlevelmenuclasshover ";}

HTML:

<TD class = "firstlevelmenuclass" id = "firstlevelmenu0" onclick = "onfirstmenuchangebg (this ); "> <a href = '#'> test 0 </a> </TD> <TD class =" firstlevelmenuclass "id =" firstlevelmenu1 "onclick =" onfirstmenuchangebg (this ); "> <a href = '#'> Test 1 </a> </TD> <TD class =" firstlevelmenuclass "id =" firstlevelmenu2 "onclick =" onfirstmenuchangebg (this ); "> <a href = '#'> Test 2 </a> </TD>

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.