首页 > 编程语言 > 详细

Change :hover CSS properties with JavaScript

时间:2017-03-01 16:36:53      阅读:183      评论:0      收藏:0      [点我收藏+]

I need to find a way to change CSS :hover properties using JavaScript.

For example, suppose I have 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 to change 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 do I change these <td>‘s :hover background using JavaScript?


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

var css = ‘table td:hover{  padding: 0px; border: 0px; color: rgb(48, 51, 54);">;
style = document.createElement(‘style‘);

if (style.styleSheet) {
    style.styleSheet.cssText = css;
} else {
    style.appendChild(document.createTextNode(css));
}

document.getElementsByTagName(‘head‘)[0].appendChild(style);

Change :hover CSS properties with JavaScript

原文:http://www.cnblogs.com/woodyliang/p/6484646.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!