javascript - 我们的一段 JavaScript 代码有问题

标签 javascript html css

我编写了这段代码并得到了以下结果但是有一个问题,如果我们删除复选框然后单击按钮,结果将不会更新!请帮助我完成这个程序...谢谢:)

const inputs = document.querySelectorAll(".inputGroup input");
const icons = document.querySelectorAll(".icons i");
let btn = document.getElementById("btn");
btn.addEventListener("click", function (e) {
inputs.forEach(function (inputs) {
     if (inputs.checked === true) {
         icons.forEach(function(i){
             if(inputs.dataset.id === i.getAttribute('class'))
             {
             i.style.display = 'block'
             }
         })
     }
     
   })
});

    
.icons i 
{
 display: none;
}
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"
        integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>

<body>
    <div class="inputGroup">
        <input data-id="fa fa-user" type="checkbox" id="user">
        <label for="user">user</label>
        <input data-id="fa fa-tree" type="checkbox" id="tree">
        <label for="tree">tree</label>
        <input data-id="fa fa-lock" type="checkbox" id="lock">
        <label for="fa fa-lock">lock</label>
        <button id="btn">Button</button>
    </div>
    <div class="icons">
        <i class="fa fa-user"></i>
        <i class="fa fa-tree"></i>
        <i class="fa fa-lock"></i>
    </div>
    <script src="./app.js"></script>
</body>

</html>

最佳答案

诀窍是,如果未选中该复选框,则“取消显示”图标。 我更改了代码的一些行以使其更具可读性(使用 find 数组方法):

const inputs = document.querySelectorAll(".inputGroup input");

// make icons an array to be able to use the .find() method on it
const icons = Array.from(document.querySelectorAll(".icons i"));

let btn = document.getElementById("btn");

btn.addEventListener("click", function(e) {
  inputs.forEach(function(input) {
    const inputId = input.dataset.id;

    // find the associated icon to the input 
    const icon = icons.find(tmpicon => inputId === tmpicon.className);

    // display the icon if the input is checked
    if (input.checked) {
      icon.style.display = "block";
    }
    // don't display it if the input isn't checked
    else {
      icon.style.display = "none";
    }
  });
});
.icons i {
  display: none;
}
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body>
  <div class="inputGroup">
    <input data-id="fa fa-user" type="checkbox" id="user">
    <label for="user">user</label>
    <input data-id="fa fa-tree" type="checkbox" id="tree">
    <label for="tree">tree</label>
    <input data-id="fa fa-lock" type="checkbox" id="lock">
    <label for="fa fa-lock">lock</label>
    <button id="btn">Button</button>
  </div>
  <div class="icons">
    <i class="fa fa-user"></i>
    <i class="fa fa-tree"></i>
    <i class="fa fa-lock"></i>
  </div>
  <script src="./app.js"></script>
</body>

</html>

关于javascript - 我们的一段 JavaScript 代码有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74171888/

相关文章:

php - 使用 htaccess 强制重写 url 以提供 css 的绝对路径

javascript - 无法在 Vue 指令中添加/删除/切换元素类?

javascript - getElementById 不起作用 - JavaScript

java - 服务器为 url openStream() 返回 403

html - 你如何防止 float 元素的 parent 崩溃?

reactjs - 使父组件中的两个子组件具有相同的高度

javascript - 正则表达式删除空格

Javascript 运算符无法将整数与零进行比较(例如 10、100、1000 等)?

javascript - 将 url 复制到剪贴板后显示成功消息

php - 为什么我不能在 HTML 表格中指定单元格的宽度和高度?