Change:hover CSS Properties with JavaScript

Source: Internet
Author: User

I need to find a by-change css:hover properties using JavaScript.

For example, suppose I has this HTML code:

<table>  <tr>    <td>Hover 1</td> <td>Hover 2</td> </tr></table>

And the following CSS code:

table td:hover {background:#ff0000;}

I would like-to-use JavaScript-the <td> hover properties to, say, background: #00ff00. Know I could access the style background property using JavaScript as:

document.getElementsByTagName("td").style.background="#00ff00";

But I don ' t know of a JavaScript equivalent for:hover. How does I change these <td> ' S:hover background using JavaScript?


Pseudo classes ( like:hover) never refer to a element, but to any element that satisfies the conditions of the stylesheet rule. You need toEdit the stylesheet ruleappend a new rule, or add a new stylesheet that includes the new:hoverrule.

VarCss= ' Table td:hover{padding:0px; border:0px; Color:rgb (48, 51, 54); " >;Style=Document.CreateElement(' Style ');If (Style.StyleSheet) {Style.StyleSheet.csstext = Css;} else {. Appendchild (document. Createtextnodecss}document. ( ' head ' 0 appendchild (style     

Change:hover CSS properties with JavaScript

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.