简体   繁体   English

在JavaScript中应用CSS

[英]Applying CSS in JavaScript

I need help in applying CSS to my Javascript. 在将CSS应用于Javascript时,我需要帮助。 As I am not good at this, I just want to ask all of you who know. 由于我不擅长此事,所以我只想问所有认识的人。 Anyway, I use JS in my hover of menu. 无论如何,我在菜单悬停中使用JS。 Below is the code: 下面是代码:

$(function(){
     $('a img').hover(function(){
        $(this).attr('src', $(this).attr('src').replace('_off', '_on'));
          }, function(){
             if (!$(this).hasClass('currentPage')) {
             $(this).attr('src', $(this).attr('src').replace('_on', '_off'));
        }
   });
});

In my HTML, it looks like this: 在我的HTML中,它看起来像这样:

<tr>
<td valign="top"><a href="#"><img src="img/tabs_01_off.png" width="229" alt="tabs_01"/></a></td>
<td valign="top"><a href="#"><img src="img/tabs_02_off.png" width="229" alt="tabs_02"/></a></td>
<td valign="top"><a href="#"><img src="img/tabs_03_off.png" width="229" alt="tabs_03"/></a></td>
<td valign="top"><a href="#"><img src="img/tabs_04_off.png" width="229" alt="tabs_04"/></a></td>
</tr>

The hover works for all the tabs, but I wanted the tabs 1 and 4 when hovered, just covers the content behind. 鼠标悬停适用于所有选项卡,但是我希望将鼠标悬停在选项卡1和4时,只需覆盖后面的内容即可。 It should not move the contents going down, but that's what happens right now actually. 它不应使内容下降,但这实际上就是现在发生的情况。 I was advised to use position:relative and z-index: -1 because when using overflow, it may not work in IE6. 建议我使用position:relativez-index: -1因为使用溢出时,它可能无法在IE6中使用。 So please help, thanks! 所以请帮忙,谢谢!

You can use this to add a class 你可以用它来添加一个类

$(this).addClass("myClass");

If you really want to use the .css syntax you need one call per style change like this: 如果您确实想使用.css语法,则每次样式更改都需要一个调用,如下所示:

  $(this).css('position','fixed');
  $(this).css('z-index','1');

Here is a jsfiddle of your code with a guess of what css you want to change. 这是您的代码的jsfiddle,其中包含您要更改的CSS的猜测。 jsfiddle.net/G4BgT Please provide more information about what you want to happen. jsfiddle.net/G4BgT请提供有关您想要发生的事情的更多信息。

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

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