[英]A button do thing on clicking & undo it on another click
Hello,你好,
I want to achieve the following:我想实现以下目标:
When Button is clicked CSS Style changes to display: block on second click must be changed to display: none.单击按钮时,CSS 样式更改为显示:第二次单击时的块必须更改为显示:无。 And it must be looped而且必须循环
How to achieve that?如何做到这一点?
I managed to create this code which adds css style when clicked(but I can't make second click event):我设法创建了这个代码,它在点击时添加了 css 样式(但我不能进行第二次点击事件):
const button = document.querySelector('button'); button.addEventListener('click', () => { const element = document.querySelector('div.wpcs_price_info ul'); element.setAttribute('style', 'display:block !important'); });
Pretty easy, just add a if condition.很简单,只需添加一个 if 条件。
const button = document.querySelector("button");
let shown = false;
button.addEventListener("click", () => {
const element = document.querySelector("div.wpcs_price_info ul");
if (shown) {
element.setAttribute("style", "display:none !important");
shown = false;
} else {
element.setAttribute("style", "display:block !important");
shown = true;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.