简体   繁体   English

如何使用 javascript 切换遍历所有类?

[英]How to iterate through all classes with a javascript toggle?

This is a toggle switch that when unchecked, will display 'min'.这是一个切换开关,未选中时将显示“min”。 If checked, then 'min' will switch to 'max' and will toggle back and forth.如果选中,则“min”将切换到“max”并来回切换。

My expected result is that however many times this checkbox is on the page, the function will work across the entire page.我的预期结果是,无论此复选框在页面上出现多少次,该功能都将在整个页面上运行。

 var x = document.getElementsByClassName("text"); function queryToggle() { for (let i = 0; i < x.length; i++) { if (x.innerHTML === "min") { x.innerHTML = "max"; } else { x.innerHTML = "min"; } } }
 <div class="options mb-2"> <label for="toggleQuery">Use 'max-width'?</label> <input type="checkbox" name="toggleQuery" id="toggleQuery" onclick="queryToggle()" /> </div> <span class="text">min</span>

I have adjusted your code and included comments.我已经调整了您的代码并包含了注释。

If you want to re-use the checkbox then simply change the id of each new checkbox you create and it will work across the page.如果您想重复使用该复选框,只需更改您创建的每个新复选框的 ID,它将在整个页面上工作。

Run the snippet below:运行下面的代码片段:

 //your span var x = document.getElementsByClassName("text"); //here we are passing "input" as a function parameter function queryToggle(input) { //checkbox will take the id of any new input we create on the page //the id must change for each new checkbox var checkbox = document.getElementById(input.id); for (let i = 0; i < x.length; i++) { //check if checkbox is checked, if checked then change innerHTML to "max" if (checkbox.checked == true) { x[i].innerHTML = "max"; // else change innerHTML to "min" } else { x[i].innerHTML = "min"; } } }
 <div class="options mb-2"> <label for="toggleQuery">Use 'max-width'?</label> <input type="checkbox" name="toggleQuery" id="toggleQuery1" onChange="queryToggle(this)" /> <input type="checkbox" name="toggleQuery" id="toggleQuery2" onChange="queryToggle(this)" /> <input type="checkbox" name="toggleQuery" id="toggleQuery3" onChange="queryToggle(this)" /> <input type="checkbox" name="toggleQuery" id="toggleQuery4" onChange="queryToggle(this)" /> </div> <span class="text">min</span> <span class="text">min</span> <span class="text">min</span> <span class="text">min</span> <span class="text">min</span>

Using you current code, try this:使用您当前的代码,试试这个:

function queryToggle() {
    let x = document.getElementsByClassName("text");
    for (let i = 0; i < x.length; i++) {
        console.log(x[i])
        if (x[i].innerHTML === "min") {
            x[i].innerHTML = "max";
        } else {
            x[i].innerHTML = "min";
        }
    }
}

Since x is an 'array-like' object you have to iterate over each item in it by its index, you were operating on the entire array, if that makes sense.由于x是一个“类数组”对象,您必须通过其索引迭代其中的每个项目,如果有意义的话,您正在对整个数组进行操作。

Shorter version较短的版本

function queryToggle() {
    let x = document.getElementsByClassName("text");
    for (let i = 0; i < x.length; i++) {
        x[i].innerHTML = x[i].innerHTML === "min" ? "max" :"min";
    }
}

Another shorter version with spread operator no need to create the x array unless you need it somewhere else, downside is that you will be accessing the DOM on every call另一个带有扩展运算符的较短版本无需创建x数组,除非您在其他地方需要它,缺点是您将在每次调用时访问 DOM

function queryToggle() {
    [...document.getElementsByClassName("t_text")].forEach((i)=>
        i.innerHTML = i.innerHTML === "min" ? "max" :"min")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM