2 回答
TA贡献1813条经验 获得超2个赞
第一:您的函数function rememberedHovered(leitnerbox)和其他函数接受参数,但您不会在悬停事件上传递它们<button onmouseover="didNotRememberHovered()" onmouseout="didNotRememberNotHovered()" class="btn "> Did Not Remember </button>。因为leitnerbox和actualLeitnerbox是全局变量——你真的不需要你的函数将它们作为参数。
第二:您没有更改按钮状态的功能。这部分代码
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < leitnerbox; i++) {
btns[i].classList.add("active");
}
只会在第一次加载时起作用,但当您将鼠标悬停在按钮上时不会改变任何东西。所以你也需要它作为一个函数并将它放在你的 onHover 函数中。你还需要稍微改变一下这部分。由于按钮的数量active发生变化,您将不得不以某种方式停用它们。
我希望这是您想要的功能:
var actualLeitnerbox = 4;
var leitnerbox = actualLeitnerbox;
function rememberedHovered() {
leitnerbox += 1;
activate();
}
function rememberedNotHovered() {
leitnerbox = actualLeitnerbox;
activate();
}
function didNotRememberHovered() {
leitnerbox = 1;
activate();
}
function didNotRememberNotHovered() {
leitnerbox = actualLeitnerbox;
activate();
}
function activate(){
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for(let i = 0; i < btns.length; i++){
btns[i].classList.remove("active");
}
for (var i = 0; i < leitnerbox; i++) {
btns[i].classList.add("active");
}
}
activate();
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
}
/* Style the active class, and buttons on mouse-over */
.active, .btn:hover {
background-color: #666;
color: white;
}
<button onmouseover="rememberedHovered()" onmouseout="rememberedNotHovered()" class="btn "> Remembered </button>
<button onmouseover="didNotRememberHovered()" onmouseout="didNotRememberNotHovered()" class="btn "> Did Not Remember </button>
<div id="myDIV">
<button class="btn">1</button>
<button class="btn">2</button>
<button class="btn">3</button>
<button class="btn">4</button>
<button class="btn">5</button>
<button class="btn">6</button>
<button class="btn">7</button>
</div>
TA贡献1993条经验 获得超5个赞
你应该让每个功能做特定的任务
此外,在这种情况leitnerbox下actualLeitnerbox是undefined,因为在 HTML 中,您调用这些函数时不带任何参数
function rememberedHovered(leitnerbox) {
// ...
}
function rememberedNotHovered(leitnerbox, actualLeitnerbox) {
// ...
}
function didNotRememberHovered(leitnerbox) {
// ...
}
function didNotRememberNotHovered(leitnerbox, actualLeitnerbox) {
// ...
}
下面的片段应该可以帮助你
var actualLeitnerbox = 4;
var leitnerbox = actualLeitnerbox;
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
function hover(i) {
btns[i].classList.add("active");
}
function unhover(i) {
btns[i].classList.remove("active");
}
function hoverAll() {
for (var i = 0; i < actualLeitnerbox; i++) hover(i)
}
function unhoverAll() {
for (var i = 0; i < actualLeitnerbox; i++) unhover(i)
}
function rememberedHovered() {
hover(actualLeitnerbox)
}
function rememberedNotHovered() {
unhover(actualLeitnerbox)
}
function didNotRememberHovered() {
unhoverAll()
hover(0)
}
function didNotRememberNotHovered() {
unhoverAll()
hoverAll()
}
for (var i = 0; i < leitnerbox; i++) {
btns[i].classList.add("active");
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
}
/* Style the active class, and buttons on mouse-over */
.active,
.btn:hover {
background-color: #666;
color: white;
}
#myDIV {
margin-top: 5rem;
}
<button onmouseover="rememberedHovered()" onmouseout="rememberedNotHovered()" class="btn "> Remembered </button>
<button onmouseover="didNotRememberHovered()" onmouseout="didNotRememberNotHovered()" class="btn "> Did Not Remember </button>
<div id="myDIV">
<button class="btn">1</button>
<button class="btn">2</button>
<button class="btn">3</button>
<button class="btn">4</button>
<button class="btn">5</button>
<button class="btn">6</button>
<button class="btn">7</button>
</div>
添加回答
举报