Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中悬停时,如何将背景颜色更改为特定类_Javascript_Html_Css - Fatal编程技术网

在Javascript中悬停时,如何将背景颜色更改为特定类

在Javascript中悬停时,如何将背景颜色更改为特定类,javascript,html,css,Javascript,Html,Css,我有三个div,其类别为colorBox。我想把鼠标悬停在它们上面,当我这样做的时候,我想让它们中的每一个都将背景颜色更改为不同的颜色。问题是。。我不知道怎么做。我假设您需要使用this关键字。但是,我觉得我用得不对 CSS .colorBox{宽度:200px;最小高度:300px;颜色:白色; 背景:黑色;显示:内联块;填充:0 7px;文本对齐:居中;} .colorBox h2{边框底部:2px纯白;} HTML 文本标题不对称wolf活版,照片亭cornhole占领纹身波特兰fan

我有三个div,其类别为colorBox。我想把鼠标悬停在它们上面,当我这样做的时候,我想让它们中的每一个都将背景颜色更改为不同的颜色。问题是。。我不知道怎么做。我假设您需要使用this关键字。但是,我觉得我用得不对

CSS


.colorBox{宽度:200px;最小高度:300px;颜色:白色;
背景:黑色;显示:内联块;填充:0 7px;文本对齐:居中;}
.colorBox h2{边框底部:2px纯白;}
HTML

文本标题不对称wolf活版,照片亭cornhole占领纹身波特兰fanny pack酿酒厂内脏屋顶派对博客。Health goth cray four loko法兰绒8位有机gochujang背心。Keytar franzen mumblecore,厌倦了stumptown etsy冥想YOLO cray 3狼月背心弹出式poutine纹身奥斯汀。破旧别致的布鲁克林keytar normcore无论什么
文本标题不对称wolf活版,照片亭cornhole占领纹身波特兰fanny pack酿酒厂内脏屋顶派对博客。Health goth cray four loko法兰绒8位有机gochujang背心。Keytar franzen mumblecore,厌倦了stumptown etsy冥想YOLO cray 3狼月背心弹出式poutine纹身奥斯汀。破旧别致的布鲁克林keytar normcore无论什么
文本标题不对称wolf活版,照片亭cornhole占领纹身波特兰fanny pack酿酒厂内脏屋顶派对博客。Health goth cray four loko法兰绒8位有机gochujang背心。Keytar franzen mumblecore,厌倦了stumptown etsy冥想YOLO cray 3狼月背心弹出式poutine纹身奥斯汀。破旧别致的布鲁克林keytar normcore无论什么
Javascript

(function(){

  var a = document.getElementsByClassName('colorBox');
  this.a = a;  
  this.style.background = 'black';

  function hoverColor()
  {
    if (this.a == a[0])
    {
     this.style.background = 'green';

    }
    else if(this.a == a[1])
    {
      this.style.background = 'blue';
    }
    else if(this.a == a[2])
    {
      this.style.background = 'red';
    }
  }

  for(var i = 0; i < a.length; ++i)
  {
    a[i].addEventListener('mouseover', hoverColor.bind(this));
  }

})();
(函数(){
var a=document.getElementsByClassName('colorBox');
这个a=a;
this.style.background='black';
函数hoverColor()
{
if(this.a==a[0])
{
this.style.background='green';
}
else if(this.a==a[1])
{
this.style.background='blue';
}
else if(this.a==a[2])
{
this.style.background='red';
}
}
对于(变量i=0;i
JavaScript中的关键字
this
,该行为是运行时上下文

例如:

var f = function() {
  var name = "OUT";
  return function() {
    console.log(this.name)
  }
}
f()()  // undefined  this.name == window.name
结果将是未定义的,为什么会这样?我们运行函数
f
并返回一个闭包函数,然后继续处理,但这次
this
引用窗口对象

var name = "WINDOW"
var f = function() {
  var name = "OUT";
  return function() {
    console.log(this.name)
  }
 }
 f()()  // WINDOW
如何解决这个问题,请通过
that
引用
this

var f = function() {
  var name = "OUT";
  var that = this
  return function() {
    console.log(that.name)
  }
}
f()()  // OUT
更多信息


(对不起,我的英语总是狗屎。)

JavaScript中的关键字
this
表示行为是运行时上下文

例如:

var f = function() {
  var name = "OUT";
  return function() {
    console.log(this.name)
  }
}
f()()  // undefined  this.name == window.name
结果将是未定义的,为什么会这样?我们运行函数
f
并返回一个闭包函数,然后继续处理,但这次
this
引用窗口对象

var name = "WINDOW"
var f = function() {
  var name = "OUT";
  return function() {
    console.log(this.name)
  }
 }
 f()()  // WINDOW
如何解决这个问题,请通过
that
引用
this

var f = function() {
  var name = "OUT";
  var that = this
  return function() {
    console.log(that.name)
  }
}
f()()  // OUT
更多信息


(对不起,我的英语总是很糟糕。)

最简单的方法是使用jQuery:

$(".ColorBox").hover(function() {
        $(".ColorBox").css("background", newColor);
    },
    function() {
        $(".ColorBox").css("background", "black");
    });

当鼠标移动到元素上时,将调用第一个函数,当鼠标离开时,将调用第二个函数。它是mouseenter和mouseexit的组合。由于您希望为此类的每个元素更改它,因此我们使用类本身,而不是“this”。

最简单的方法是使用jQuery:

$(".ColorBox").hover(function() {
        $(".ColorBox").css("background", newColor);
    },
    function() {
        $(".ColorBox").css("background", "black");
    });
当鼠标移动到元素上时,将调用第一个函数,当鼠标离开时,将调用第二个函数。它是mouseenter和mouseexit的组合。因为您想为这个类的每个元素更改它,所以我们使用类本身,而不是“this”。

(函数(){
var a=document.getElementsByClassName('colorBox');
函数addHover(索引){
返回函数(){
var backgroundColor='黑色';
开关(索引){
案例0:
背景颜色='绿色';
打破
案例1:
背景颜色='蓝色';
打破
案例2:
背景颜色='红色';
打破
}
if(a[索引]){
a[index].style.background=backgroundColor;
}
};
}
对于(变量i=0;i

(函数(){
var a=document.getElementsByClassName('colorBox');
函数addHover(索引){
返回函数(){
var backgroundColor='黑色';
开关(索引){
案例0:
背景颜色='绿色';
打破
案例1:
背景颜色='蓝色';
打破
案例2:
背景颜色='红色';
打破
}
if(a[索引]){
a[index].style.background=backgroundColor;
}
};
}
对于(变量i=0;i
是否有任何原因使您无法使用CSS
:hover
选择器执行此操作?您是在尝试更改整个文档的背景还是在悬停时分别更改每个
的背景?对于前者,全局对象(默认值为
this
)没有要修改的
.style
对象。也许你的意思是
document.body.style.background=…
。这里有一个很酷的资源可以帮助你:你有什么理由不能使用CSS
:hover
选择器来做这件事吗?你是否在尝试更改e