如何在Javascript中悬停,将背景颜色更改为特定的类
How do you change a background color to a specific class with hovering in Javascript
我有三个div,类为colorBox。我想把鼠标悬停在它们中的每一个上面,当我这样做的时候,我想让它们中的每个都把背景颜色改成不同的。问题是…我不知道该怎么做。我假设您需要使用this关键字。但是,我认为我用得不对
CSS
<style type="text/css">
.colorBox{ width:200px; min-height:300px; color:white;
background:black; display:inline-block; padding:0 7px; text-align:center; }
.colorBox h2 { border-bottom:2px solid white;}
</style>
HTML
<div class="colorBox"><h2>Text Header</h2><p>Asymmetrical wolf letterpress, photo booth cornhole occupy tattooed portland fanny pack distillery offal roof party blog. Health goth cray four loko flannel 8-bit organic, gochujang waistcoat. Keytar franzen mumblecore, ennui stumptown etsy meditation YOLO cray 3 wolf moon waistcoat pop-up poutine tattooed austin. Shabby chic brooklyn keytar normcore whatever <p></div>
<div class="colorBox"><h2>Text Header</h2><p>Asymmetrical wolf letterpress, photo booth cornhole occupy tattooed portland fanny pack distillery offal roof party blog. Health goth cray four loko flannel 8-bit organic, gochujang waistcoat. Keytar franzen mumblecore, ennui stumptown etsy meditation YOLO cray 3 wolf moon waistcoat pop-up poutine tattooed austin. Shabby chic brooklyn keytar normcore whatever <p></div>
<div class="colorBox"><h2>Text Header</h2><p>Asymmetrical wolf letterpress, photo booth cornhole occupy tattooed portland fanny pack distillery offal roof party blog. Health goth cray four loko flannel 8-bit organic, gochujang waistcoat. Keytar franzen mumblecore, ennui stumptown etsy meditation YOLO cray 3 wolf moon waistcoat pop-up poutine tattooed austin. Shabby chic brooklyn keytar normcore whatever <p></div>
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));
}
})();
(function(){
var a = document.getElementsByClassName('colorBox');
function addHover(index) {
return function(){
var backgroundColor = 'black';
switch (index) {
case 0:
backgroundColor = 'green';
break;
case 1:
backgroundColor = 'blue';
break;
case 2:
backgroundColor = 'red';
break;
}
if (a[index]) {
a[index].style.background = backgroundColor;
}
};
}
for (var i = 0; i < a.length; ++i) {
a[i].addEventListener('mouseover', addHover(i));
}
})();
JavaScript中的关键字this
表示行为是运行时上下文。
例如:
var f = function() {
var name = "OUT";
return function() {
console.log(this.name)
}
}
f()() // undefined this.name == window.name
结果会是undefined
,为什么?我们运行函数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
了解更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
(对不起,我的英语总是很糟糕。)
最简单的方法是使用jQuery:
$(".ColorBox").hover(function() {
$(".ColorBox").css("background", newColor);
},
function() {
$(".ColorBox").css("background", "black");
});
当鼠标在元素上移动时,第一个函数将被调用,第二个函数将在元素离开时被调用。它是mouseenter和mouseexit的组合。由于您想为这个类的每个元素更改它,所以我们使用类本身,而不是"this"。
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用jquery更改html中的背景颜色
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- JS幻灯片与CSS背景颜色变化
- DT数据表背景颜色为黑色,但仍有悬停和选择颜色
- Javascript通过列表项的函数和css来更改背景颜色
- Javascript没有从数组中选择背景颜色
- 使用圆中的数组更改背景颜色项目
- 如何用jquery动画改变背景颜色,就像一个过渡
- 使用jquery交换并保存背景颜色
- j查询如何设置/取消设置下拉菜单的背景颜色
- 使用angularjs根据时间更改背景颜色
- 设置Dojo量表的背景颜色
- 如何使用颜色选择器来更改背景中的文本
- charts js更改折线图轴的字体大小/颜色和背景线
- javascript样式的背景颜色在设置时闪烁.ASP.NET
- 在背景颜色背景图像之间分层文本
- sliicknav菜单,每个li都有特定的颜色背景
- 尝试根据当前背景更改背景颜色:背景颜色没有值
- jQuery中的颜色/背景颜色改变动画