简体   繁体   English

一个按钮在单击时执行操作并在另一次单击时撤消它

[英]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.

相关问题 提交按钮单击将其值更改为另一件事 - Submit button clicking changing the value of it to another thing 触发共享按钮,单击另一个按钮 - Trigger the share button click on clicking another button 通过单击另一个按钮来委派一个按钮单击事件 - Delegating a button click event by clicking another button 单击一个按钮以触发另一个按钮的延迟单击 - Clicking a button to trigger a delayed click for another buttons 通过单击另一个按钮来撤消按钮动画,但前提是已经播放了动画 - Undo button animation by clicking another button but only if the animation has been played ReactJS获取Enter键以执行与按钮单击相同的操作 - ReactJS getting an Enter Key to do the same thing as the button click 单击一个按钮可以取消jQuery中另一个(外部)按钮的单击事件 - Clicking on One Button Cancels a Click Event of Another (Outer) Button in jQuery 将单个单击事件绑定到多个按钮时,如何使被单击的按钮做一件事,而其他所有按钮做另一件事? - When binding a single click event to multiple buttons, how can I make the clicked button do one thing and all the other buttons do another? 单击另一项时将焦点设置为一件事? - Set focus to one thing when clicking on another? Ember.js - 单击一个按钮可触发另一个按钮上的点击事件 - Ember.js - Clicking on one button to trigger click event on another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM