如何在点击事件中更改多种样式?

huangapple go评论98阅读模式
英文:

How can I change multi styles onClick event?

问题

需要更改5个案例的样式,通过点击事件触发。例如,如果用户点击按钮,需要更改3种不同的样式。

function myFunction() {
    document.getElementById("pa-menu").classList.toggle("responsive-menu-action");
}
window.onclick = function(event) {
    if (!event.target.matches('.pa-header-right-responsive-menu-btn')) {
        var dropdowns = document.getElementsByClassName("pa-menu");
        for (let i = 0; i < dropdowns.length; i++) {
            let openDropdown = dropdowns[i];
            if (openDropdown.classList.contains('responsive-menu-action')) {
                openDropdown.classList.remove('responsive-menu-action');
            }
        }
    }
}
.pa-menu {
    width: 150px;
}
.customa {
    background: red;
}
.customb {
    font-size: 14px;
}
英文:

I need to change style for 5 case with onclick event. for example,
if the user clicks on btn, need to change more than 3 different style.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function myFunction() {
    document.getElementById(&quot;pa-menu&quot;).classList.toggle(&quot;responsive-menu-action&quot;);
}
window.onclick = function(event) {
    if (!event.target.matches(&#39;.pa-header-right-responsive-menu-btn&#39;)) {
        var dropdowns = document.getElementsByClassName(&quot;pa-menu&quot;);
        for (let i = 0; i &lt; dropdowns.length; i++) {
            let openDropdown = dropdowns[i];
            if (openDropdown.classList.contains(&#39;responsive-menu-action&#39;)) {
                openDropdown.classList.remove(&#39;responsive-menu-action&#39;);
            }
        }
    }
}

<!-- language: lang-css -->

.pa-menu {
    width: 150px;
}
.customa {
    background: red;
}
.customb {
    font-size: 14px;
}

<!-- end snippet -->

答案1

得分: 1

以下是翻译好的部分:

这个问题不太清楚。

下面的代码在点击按钮时切换了按钮的 3 个类名。

let btn = document.getElementById("btn");
btn.addEventListener("click", function (event) {
    btn?.classList.toggle("border-blue");
    btn?.classList.toggle("background-orange");
    btn?.classList.toggle("border-radius");
});
.border-blue {
    border: 5px solid blue;
}
.background-orange {
    background: orange;
}
.border-radius {
    border-radius: 20px;
}
<button id="btn" class="background-orange">Click me</button>
英文:

The question is not clear.

The following code toggles 3 class names for the button when clicking on it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let btn = document.getElementById(&quot;btn&quot;);
btn.addEventListener(&quot;click&quot;, function (event) {
    btn?.classList.toggle(&quot;border-blue&quot;);
    btn?.classList.toggle(&quot;background-orange&quot;);
    btn?.classList.toggle(&quot;border-radius&quot;);
});

<!-- language: lang-css -->

.border-blue {
    border: 5px solid blue;
}
.background-orange {
    background: orange;
}
.border-radius {
    border-radius: 20px;
}

<!-- language: lang-html -->

&lt;button id=&quot;btn&quot; class=&quot;background-orange&quot;&gt;Click me&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 08:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定