【问题标题】:How to move a div when a checkbox is checked in .html/.css在 .html/.css 中选中复选框时如何移动 div
【发布时间】:2021-08-04 21:53:21
【问题描述】:

我正在使用.css和.html制作网页,并具有DIV“ RowContainer”,当检查复选框时,我想在DIV中移动,然后如果未选中,则最好移动。另外我计划有很多这样的行,所以我可以给它们单独的名称,以便我可以单独引用它们吗?

编辑:我找到了解决方案,感谢@mplungjan,我在下面添加了对我有用的代码。

            <script type="text/javascript">
                const selected = document.querySelector(".selected");
                const main =    document.getElementById("tablecontainer")
                main.addEventListener("click", function(e) {
                const tgt = e.target;
                if (tgt.classList.contains("move")) {
                    const rowContainer = tgt.closest(".rowcontainer");
                    if (tgt.checked) selected.append(rowContainer)
                    else main.append(rowContainer)
                    }
                })
                selected.addEventListener("click", function(e) {
                const tgt = e.target;
                if (tgt.classList.contains("move")) {
                    const rowContainer = tgt.closest(".rowcontainer");
                    main.append(rowContainer)
                    }
                })
            </script>
.selected{
    margin:auto;
    width: 80%px;
    height: auto;
    min-height: 30px;
    margin-bottom: 1%;
}

#tablecontainer {
    min-height:200px;
}

.rowcontainer {
    margin: auto;
    width: 80%;
    height: 30px;
}

.name {
    line-height: 30px;
    float: left;
    width: 60%;
    height: 100%;
    padding-left: 15px
}

.ako {
    line-height: 30px;
    text-align: center;
    float: left;
    width: 20%;
    height: 100%;
}

.select {
    line-height: 30px;
    text-align: center;
    float: left;
    width: 20%;
    height: 100%;
}
<div id = "selected" class = "selected"></div>
<div id="tablecontainer">
                    <div class = "rowcontainer" style = "background-      color: #e6e6e6;">
                        <div class = "name"><t>Patrick Star</t></div>
                        <div class = "ako"><t>13GWZ</t></div>
                        <div class = "select">
                            <input type = "checkbox" class = "checkbox move">
                        </div>

【问题讨论】:

    标签: html css checkbox


    【解决方案1】:

    试试这个

    无需命名。但你需要 JavaScript

    如果您需要保持订单,则需要列出它们并可能使用数据属性

    const selected = document.querySelector(".selected");
    const main = document.getElementById("mainContainer")
    main.addEventListener("click", function(e) {
      const tgt = e.target;
      if (tgt.classList.contains("move")) {
        const rowContainer = tgt.closest(".rowcontainer"); // the container the checkbox is in
        if (tgt.checked) selected.append(rowContainer)
        else main.append(rowContainer)
      }
    })
    .selected {
      margin: auto;
      max-width: 800px;
      height: auto;
      min-height: 30px;
      margin-bottom: 1%;
    }
    
    .rowcontainer {
      margin: auto;
      width: 80%;
      height: 30px;
    }
    
    .name {
      line-height: 30px;
      float: left;
      width: 60%;
      height: 100%;
      padding-left: 15px
    }
    
    .ako {
      line-height: 30px;
      text-align: center;
      float: left;
      width: 20%;
      height: 100%;
    }
    
    .select {
      line-height: 30px;
      text-align: center;
      float: left;
      width: 20%;
      height: 100%;
    }
    
    .checkbox {
      height: 17px;
      width: 17px;
      margin: auto;
      margin-top: 6px;
    }
    <div id="mainContainer">
      <div class="selected"></div>
    
      <div class="rowcontainer">
        <div class="name">
          <bt>Name</bt>
        </div>
        <div class="ako">
          <bt>Ako</bt>
        </div>
        <div class="select">
          <bt>Select</bt>
          <input type="checkbox" class="checkbox move">
        </div>
      </div>
    </div>

    【讨论】:

    • 您好,感谢您的回复。我已将 javascript 添加到我的代码中并对我的 ccs/html 进行了更改。当我选中该框时,没有任何反应,rowcontainer 不会移动,但是当我取消选择它时,它会将其附加到主容器的末尾。我在“if (tgt.checked) selected.append(rowContainer)”这一行上收到此错误“Uncaught TypeError: Cannot read property 'append' of null at HTMLDivElement.”。有什么想法可以解决这个问题?
    • 好吧,我意识到我的 javascript 放错了地方,所以我已经修复了这个问题,现在该行在选中时移动到选中状态,但在未选中时它不会移回。这次没有错误消息。有什么想法吗?
    • 好的,我也想通了,我只需要添加另一个“eventListner”。非常感谢您的帮助,此解决方案完美运行。我将用我的解决方案更新帖子。
    【解决方案2】:

    在您的页面上使用此脚本。

     const maindiv = document.querySelector('.selected'); // this is your maindiv 'selected'
            maindiv.style.backgroundColor = 'green';  // colored so that change is visible
    
            const row = document.querySelector('.rowcontainer'); // this is your row
            row.style.backgroundColor = 'cornflowerblue';   // colored so that change is visible
    
            const checkbox = document.querySelector('.checkbox');   // this is your checkbox
    
            window.onload = check;          // this checks if he checkbox is checked or not when page loads up
            checkbox.onclick = check;          // this will check when user click on chekbox
    
            // the function that does all the work
            function check() {
                if (checkbox.checked) {
                    maindiv.appendChild(row);   // it will append your row to maindiv (if chekbox is checked)
                }else{
                    document.body.appendChild(row); // it will append your row to the body (if checkbox is not checked)
                }
                
            }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2021-02-19
      相关资源
      最近更新 更多