简体   繁体   English

使用JavaScript更改多个元素悬停背景颜色?

[英]Change multiple elements hover background color using JavaScript?

I've got this working right now using multiple ID's and consequentially, CSS properties for each one. 我现在已经使用多个ID并因此分别为每个CSS属性进行了这项工作。 I'd much rather have it be automatic. 我宁愿它是自动的。 All links are displayed as blocks and floated next to each other to create a tiled look. 所有链接均显示为块,并且彼此相邻浮动以创建平铺外观。 For example: 例如:

<a href="#" id="spacetile1">Link 1</a>
<a href="#" id="spacetile2">Link 2</a>
<a href="#" id="spacetile3">Link 3</a>
<a href="#" id="spacetile4">Link 4</a>

#spacetile1 {
background-color:#FFF;}

#spacetile1:hover {
background-color:#000;} .... so on and so forth for all spacetiles

What I'm looking to do is change the hover color based on the attribute of the default background color using some if statements to save time and accuracy of looking up corresponding branding colors. 我要做的是使用一些if语句根据默认背景颜色的属性更改悬停颜色,以节省查找相应品牌颜色的时间和准确性。

if .spacetile(background-color) === #FFF
then .spacetile:hover(background-color) = #000

I want to do that for a set amount of colors so all I have to do is code the background color I'd like that specific tile to be and the hover background will be taken care of with the script. 我想对一定数量的颜色执行此操作,因此我要做的就是编码我希望特定图块成为的背景色,并且悬停背景将由脚本处理。

I've looked into getElementById but then I'd still be using multiple ID's instead of one class and everything I've read about getElementsByClassName says that it's not supported cross-browser. 我已经研究过getElementById,但是我仍然会使用多个ID而不是一个类,并且我所阅读的有关getElementsByClassName的所有内容都表示不支持跨浏览器。

Was wondering if anyone had any suggestions for simplicity and efficiency. 想知道是否有人对简单性和效率提出任何建议。

Thanks! 谢谢!

With a common class . 具有共同的阶级

<a href="#" id="spacetile1" class="space">Link 1</a>
<a href="#" id="spacetile2" class="space">Link 2</a>
<a href="#" id="spacetile3" class="space">Link 3</a>
<a href="#" id="spacetile4" class="space">Link 4</a>

Using JQuery - JQuery Mouse Events 使用JQuery - JQuery鼠标事件

$(".space").mouseover(function(){

      if($(this).css('background-color')=='#FFF000')
      {
          $(this).css('background-color','#000FFF');
      }
      //else if else if and so on...
});

$(".space").mouseout(function(){

      if($(this).css('background-color')=='#FFF000')
      {
          $(this).css('background-color','#000FFF');
      }
      //else if else if and so on...
});

Why not use a CSS class: 为什么不使用CSS类:

<a href="#" id="spacetile1" class="spacetile">...</a>
<a href="#" id="spacetile2" class="spacetile">...</a>

<style>
.spacetile { background-color: #FFF; }
.spacetile:hover { background-color: #000; }
</style>

The question is built on a faulty assumption. 这个问题是建立在错误的假设之上的。 getElementsByClassName is cross-browser for everything except IE<=8 . 对于IE<=8以外的所有内容, getElementsByClassName是跨浏览器的。 See http://caniuse.com/#feat=getelementsbyclassname (If you need to support IE<=8 , you can fake it as below): 请参阅http://caniuse.com/#feat=getelementsbyclassname (如果需要支持IE<=8 ,则可以如下伪造):

function compatGetElementsByClassName(class, tag) {
  if (document.getElementsByClassName) {
    //Use native version
    return document.getElementsByClassName(class);
  } else {
    //Fake it
    i = 0;
    subset = (typeof tag !== 'undefined')?document.getElementsByTagName(tag):document.all;
    while (element = subset[i++]) {
      if (element.className == class) {
        //your code here
      }
    }
  }
}

Just enter the class name and (optionally) the tag name ("a" in your case). 只需输入类名称和(可选)标签名称(在您的情况下为“ a”)即可。 If you don't give a tag name, it will default to document.all which is very inefficient. 如果您不提供标签名称,它将默认为document.all ,效率很低。

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

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