简体   繁体   English

如何在Javascript中引用这个CSS?

[英]How to reference this CSS in Javascript?

i have the following CSS for a mouse hover event. 我有一个鼠标悬停事件的以下CSS。 Im not sure how to refer to the #tabs ul li a:hover from within the Javascript? 我不知道如何在Javascript中引用#tabs ul li a:hover?

#tabs ul li a:hover
{
    color: #000;
    font-weight:bold;
    background-color: #0ff;
}

and i wish to swap the background color line for this Javascript code: 我希望交换此Javascript代码的背景颜色线:

<script type="text/javascript">
     hex=255;

     function fadetext(){ 
         if(hex>0) {
             hex-=11;
             document.getElementById("#tabs ul li a:hover").style.color="rgb("+hex+","+hex+","+hex+")";
             setTimeout("fadetext()",50); 
         }
         else
             hex=255;
     }
</script>

This: 这个:

document.getElementById("#tabs ul li a:hover")

isn't valid syntax, you only need to specify the id there: 语法无效,您只需要在那里指定id:

document.getElementById("tabs")

You can change the style of an element on hover something like this: 您可以在悬停时更改元素的样式:

var elem = document.getElementById("id");

elem.onmouseover = function(){
   // your code
};

Let's suppose you have assigned the id myid to your link, you can do the stuff for that like this: 假设您已将id myid分配给您的链接,您可以为此执行以下操作:

var elem = document.getElementById("myid");

elem.onmouseover = function(){
   elem.style.backgroundColor = 'color value';
   elem.style.color = 'color value';
};

Update: 更新:

Since in your code you are using loadit(this) in onclick event, you don't need to use document.getElementById because element is already referenced with this keyword, also you may want to use the onmouseover event instead of click event if you want to something to happen when element is hovered like: 因为在你的代码中你在onclick事件中使用了loadit(this) ,所以你不需要使用document.getElementById因为元素已经被this关键字引用了,你也可以想要使用onmouseover事件而不是click事件元素悬停时发生的事情如下:

<li><a href="tab-frame-workexperience.html" target="mainFrame" onmouseover="loadit(this)" >Work experience</a></li>

and then your function should look like this: 然后你的函数应该是这样的:

function loadit(elem)
{
   elem.style.color = 'color value';
}

and/or you can create the two functions for two events if you want. 和/或如果需要,您可以为两个事件创建两个函数。

Note also that you can use jQuery to do it easily and in unobstrusive fashion with hover method: 请注意 ,您可以使用jQuery通过hover方法轻松地以不引人注目的方式执行此操作:

$(function(){
  $('#tabs ul li a').hover(function(){
     $(this).css('color', '#ff0000'); // this fires when mouse enters element
  }, function(){
     $(this).css('color', '#000'); // this fires when mouse leaves element
     }
  );
});

One solution is to edit your stylesheet instead of changing the style of every element, this can be done with a simple one-liner: 一种解决方案是编辑样式表而不是改变每个元素的样式,这可以通过一个简单的单行程序来完成:

document.styleSheets[0].insertRule("#tabs ul li a:hover{rgb(255, 255, 255);}", 0);

Where the second argument specifies that the rule should be inserted first in the stylesheet. 第二个参数指定应首先在样式表中插入规则。

For IE this is done with the addRule function instead: 对于IE,这是使用addRule函数完成的:

document.styleSheets[0].addRule("#tabs ul li a:hover", "rgb(255, 255, 255)");

Update: 更新:

In your case, it would mean replacing this row: 在您的情况下,它将意味着替换此行:

document.getElementById("#tabs ul li a:hover").style.color="rgb("+hex+","+hex+","+hex+")";

with

var ss = document.styleSheets[0]; //gets the first external stylesheet in your document
if(ss.insertRule) //checks browser compatibility
  ss.insertRule("#tabs ul li a:hover{rgb("+ hex + ", " + hex + ", " + hex + ");}", 0);
else
  ss.addRule("#tabs ul li a:hover", "rgb("+ hex + ", " + hex + ", " + hex + ")");

Do you mean like this? 你的意思是这样的吗? This didn't work... 这不起作用......

#tabs ul li a:hover
{
    color: #000;
    font-weight:bold;
    <script type="text/javascript">
            hex=255;
            var elem = document.getElementById("tabs");
            elem.onmousehover = function fadetext(){ 
            if(hex>0) {
                hex-=11;
                elem.style.color="rgb("+hex+","+hex+","+hex+")";
                setTimeout("fadetext()",50); 
            }
            else
                hex=255;
            }
        </script>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM