- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要完成的是一段代码,当单击其中一个按钮时,该按钮将获得下面列出的样式,而所有其他按钮样式将返回 null。
我假设它涉及一个 bool 语句,但也许我错了。
您将在下面看到我试图使用我的“else if”语句来确定当事件目标不等于“button”时,将所有内容返回为 null。但是,这显然行不通。
我也试过“!==”,但没有用。
显然,这可以通过为每个按钮创建一个唯一的事件监听器来完成,但我想弄清楚如何动态地执行此操作。
CSS:
button {
font-family: "sans-serif";
margin: 1%;
padding: 1%;
cursor: pointer;
font-size: 22px;
}
这是我的 html:
<section id="button-container">
<button>a</button>
<button>b</button>
<button>c</button>
<button>d</button>
<button>e</button>
</section>
这是我的 Javascript:
var buttons = document.querySelectorAll('#button-container button');
buttons.forEach(button => {
button.addEventListener("click", function(e) {
if (e.target === button) {
e.target.style.backgroundColor = "blue";
e.target.style.color = "orange";
} else if (e.target != button) {
e.target.style.backgroundColor = null;
e.target.style.color = null;
}
})
})
最佳答案
我不会直接修改样式
,而是使用一个类:
button.active {
background-color: blue;
color: orange
}
当你点击一个按钮然后添加 active
类给它:
button.addEventListener("click", function(e) {
this.classList.add("active");
});
我们现在只需添加一些逻辑即可从之前单击的按钮中删除 active
类。
我们可以遍历所有按钮并从所有按钮中删除类:
buttons.forEach(button => button.classList.remove("active"));
或者我们在 DOM 中查询当前“事件”按钮并仅将其从该按钮中删除:
const activeButton = document.querySelector("#button-container button.active");
activeButton.classList.remove("active");
或者我们使用当前按钮的 parentNode
来“搜索”:
const activeButton = button.parentNode.querySelector("button.active");
或者我们使用 .getElementsByClassName()
的“魔法”,它返回具有给定 CSS 的所有元素的实时集合(它将自动更新具有给定类的当前元素集)类:
const buttons = document.querySelector('#button-container');
const activeButtons = document.querySelector("#button-container").getElementsByClassName("active");
/* ... */
button.addEventListener("click", function(ev) {
if (activeButtons.length) {
activeButtons[0].classList.remove("active"); // there should always be only _one_ active button
}
this.classList.add("active");
});
例子:
var buttons = document.querySelectorAll('#button-container button');
buttons.forEach(button => {
button.addEventListener("click", function(e) {
const activeButton = button.parentNode.querySelector("button.active");
if (activeButton) {
activeButton.classList.remove("active");
}
this.classList.add("active");
})
})
button {
font-family: "sans-serif";
margin: 1%;
padding: 1%;
cursor: pointer;
font-size: 22px;
}
button.active {
background-color: blue;
color: orange;
}
<section id="button-container">
<button>a</button>
<button>b</button>
<button>c</button>
<button>d</button>
<button>e</button>
</section>
关于javascript - 如果使用 vanilla Javascript 触发另一个 EventListener,我如何返回或使所有其他 EventListener 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59345927/
我想要完成的是一段代码,当单击其中一个按钮时,该按钮将获得下面列出的样式,而所有其他按钮样式将返回 null。 我假设它涉及一个 bool 语句,但也许我错了。 您将在下面看到我试图使用我的“else
我在我的 javascript 文件中的 for 循环中创建了一堆 div。它们具有相同的类名但不同的 ID。 我通过 for 循环添加了一个事件监听器,但我之前的分配被覆盖了,现在所有的 div 都
我为一些 创建了一个 EventListener元素,现在我想更改此特定元素的子元素的不透明度,以更改此特定元素上的 EventListener 是否为真。我如何用 jQuery 或 Javascr
我正在尝试删除一个 eventListener,但看起来我错过了什么。 为什么下面的代码不起作用,它没有从按钮中删除事件监听器。 我也试过绑定(bind)这个来传递作用域,但是也没用 class Te
我想在一个类中处理多个事件,这是我的示例: @Lazy(false) @Component public class EventListenerImpl { @EventListener
在我的应用程序中,我有“用户”。一个用户可以有多个“账户” 我的“帐户”实体上有一个监听器。它在“service.yml”文件中声明如下: account_listener: class: A
我想在我的项目中使用 CQRS 模式的元素。我想知道我是否用命令和事件做对了。 我不确定的是事件是否可以调用命令。为了更好地展示我想要做什么,我将使用图表和示例。 这是一个例子: 用户调用 TripC
所以当我点击“hartje”部分时,我正在触发一个功能。该函数正在获取名称为“food-option”的所有类。如果我点击函数触发器,它应该给“数据最喜欢的”一个值。它确实给了它一个值,但问题是我必须
我试图理解这段代码,但它没有任何意义。当点击#open_help按钮时,他正在调用handleOpen(),它调用showHelp(),它调用jQuery函数来显示帮助div,但如果您在下面看到他正在
使用 JavaScript 创建表格时,我正在尝试在循环中设置事件监听器。每次迭代中的事件监听器应调用相同的函数,但以当前索引作为参数。我在这里发现我可以将闭包与函数数组一起使用,但我必须使用数组吗?
这个问题已经有答案了: Javascript infamous Loop issue? [duplicate] (5 个回答) 已关闭 7 年前。 http://jsfiddle.net/1wqdmo
我调用一个函数,其中包含“if/else”语句,该函数根据作为参数传递的 bool 值添加或删除监听器。 我的问题是我无法通过调用相同的函数来删除先前添加的监听器 我缺少什么? function li
我正在尝试学习 HTML5 Canvas 。在出现吃 cookies 的猫 gif 之前,第一张图片需要点击七次。最初,我将 click EventListener 绑定(bind)到图像。出现吃 c
我很喜欢普通的 JS 井字游戏每个单元格都是一个按钮。为了处理点击,我将 eventListener 添加到包装 div: board.addEventListener("click", handle
我正在寻找一种简洁明了的方法来向我的每个复选框添加事件监听器,而不必重复我的 Javascript 代码,当涉及到 JS 时,我仍然不知所措,所以任何帮助都会非常有用赞赏。 这是我目前所拥有的
大家好。有人知道我们使用 addEventListener 方法时事件存储在哪里吗???示例: window.addEventListener('mousedown', this.myvar.onCl
有没有一种方法可以将事件监听器添加到输入,其中焦点从 onfocus 变为 off focus 基本上我使用 key up 为我的输入框分配了一个事件监听器,然后调用我的服务器来检查用户名是否已经在数
我正在 try catch 表单的 invalid 事件,但它似乎没有发生? https://codepen.io/MartinMuzatko/pen/VzWwxG?editors=1010 对于我的
我试图在单击 Canvas 元素时删除 eventListener: document.getElementById("canvas") .addEventListener("clic
我使用这行代码向通过 forloop 创建的 div 添加事件监听器: for(var i in mail){ //create div parent.addEventListener("
我是一名优秀的程序员,十分优秀!