英文:
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("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');
}
}
}
}
<!-- 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("btn");
btn.addEventListener("click", function (event) {
btn?.classList.toggle("border-blue");
btn?.classList.toggle("background-orange");
btn?.classList.toggle("border-radius");
});
<!-- language: lang-css -->
.border-blue {
border: 5px solid blue;
}
.background-orange {
background: orange;
}
.border-radius {
border-radius: 20px;
}
<!-- language: lang-html -->
<button id="btn" class="background-orange">Click me</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论