我是JS的初学者,正在尝试创建一个简单的函数,单击一个按钮,背景图像发生变化,但是它不起作用(我试图重新创建一个更简单的图像滑块)。我不明白为什么。
这是我的JS。我使用了绝对链接,希望它可以解决问题和警报,以查看if / else语句是否有效。
const btnLeft = document.getElementById("btn-left");
const btnRight = document.getElementById("btn-right");
btnLeft.addEventListener("click", whenClicked);
btnRight.addEventListener("click", whenClicked);
function whenClicked() {
if (btnLeft.clicked) {
document.getElementById("img2").style.backgroundImage =
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f?ixlib=rb-
1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-jimenez-A-NVHPka9Rk-unsplash.jpg')";
} else alert("no click!");
}
这是我的HTML:
<div class="slide-container">
<div class="slide" id="img1">
<button id="btn-left"> <i class="fas fa-arrow-circle-left fa-3x " ></i></button>
<button id="btn-right"><i class="fas fa-arrow-circle-right fa-3x " ></i></button>
</div>
<div class="slide" id="img2">
</div>
</div>
这是相关的CSS。
.slide-container {
display: flex;
}
.slide:nth-child(1) {
background-image: url(../img/img1.jpg);
height: 789px;
width: 100%;
flex-shrink: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.slide:nth-child(2) {
background-image: url(../img/img2.jpg);
height: 789px;
width: 100%;
flex-shrink: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
#btn-left {
position: absolute;
top: 400px;
}
#btn-right {
position: absolute;
left: 1380px;
top: 400px;
}
任何帮助深表感谢,我已经疯了几个小时了!谢谢。
编辑。在David784的帮助下,我现在有了这个:
const btnLeft = document.getElementById("btn-left");
const btnRight = document.getElementById("btn-right");
btnLeft.addEventListener("click", whenClicked);
btnRight.addEventListener("click", whenClicked);
function whenClicked(e) {
if (e.currentTarget === btnLeft ) {
document.getElementById("img1").style.backgroundImage =
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f?
ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-jimenez-
A-NVHPka9Rk-unsplash.jpg')";
}
else alert("no click!");
}
而且有效。但是当我尝试为右键添加相同的内容时,例如
function whenClicked(e) {
if (e.currentTarget === btnRight ) {
document.getElementById("img1").style.backgroundImage =
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f?
ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-
jimenez-A-NVHPka9Rk-unsplash.jpg')";
}
else alert("no click!");
}
左按钮停止工作。为什么?
您的问题听起来好像backgroundImage
是问题所在,但是我似乎无法重复(请参见下面的工作片段)。
但是我确实遇到了麻烦if (btnLeft.clicked)
...我不确定这Element.clicked
是什么...至少我以前从未见过。当我将其更改为时e.currentTarget===btnLeft
,它才起作用。(有关更多信息,请参见Event.currentTarget)。
const btnLeft = document.getElementById("btn-left");
const btnRight = document.getElementById("btn-right");
btnLeft.addEventListener("click", whenClicked);
btnRight.addEventListener("click", whenClicked);
function whenClicked(e) {
if (e.currentTarget === btnLeft) {
document.getElementById("img2").style.backgroundImage =
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-jimenez-A-NVHPka9Rk-unsplash.jpg')";
} else alert("no click!");
}
.slide-container {
display: flex;
}
.slide:nth-child(1) {
background-image: url(../img/img1.jpg);
height: 789px;
width: 100%;
flex-shrink: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.slide:nth-child(2) {
background-image: url(../img/img2.jpg);
height: 789px;
width: 100%;
flex-shrink: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
#btn-left {
position: absolute;
top: 400px;
}
#btn-right {
position: absolute;
left: 1380px;
top: 400px;
}
<div class="slide-container">
<div class="slide" id="img1">
<button id="btn-left"> <i class="fas fa-arrow-circle-left fa-3x " >left</i></button>
<button id="btn-right"><i class="fas fa-arrow-circle-right fa-3x " >right</i></button>
</div>
<div class="slide" id="img2">
</div>
</div>
*编辑:要解决有关CSS的评论,这由CSS特殊性规则解决。哪个状态:
添加到元素的内联样式(例如style =“ font-weight:bold;”)始终会覆盖外部样式表中的任何样式,因此可以认为具有最高的特异性。
这样做时element.style.backgroundImage
,这将创建一个内联样式,该样式将被覆盖。(除了我想到的!important
规则,您几乎永远不应该使用这些规则)
为了解决有关将事件传递到事件处理程序中的问题:这是使用事件时非常有用的工具,称为事件对象
当您要在多个元素上设置相同的事件处理程序并在事件发生时对所有元素执行某些操作时,e.target会非常有用。
因此,事件对象几乎可以随意命名。你可以称之为e
,event
,evnt
,或什么的。但是,它将为您提供许多非常有用的信息,例如target
点击的信息,点击的x / y坐标等。
或者在这种情况下,currentTarget与稍有不同target
,它使我们可以访问事件处理程序实际附加到的元素。(此处target
最有可能是i
标签内的button
标签)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句