简体   繁体   English

Append div 点击后行

[英]Append div to row after click

I have a form and a row of rectangles (divs) that contain variables.我有一个表格和一行包含变量的矩形(div)。 Once a rectangle is clicked the variable goes into the first form slot.单击矩形后,变量将进入第一个表单槽。 This works at the moment, however, I am trying to have the variable in the form go back to the row of rectangles after being clicked again.这目前有效,但是,我试图在再次单击后将 go 形式的变量返回到矩形行。 My appendChild doesn't seem to be working and I would appreciate any help.我的 appendChild 似乎没有工作,我将不胜感激。

html html

<div id="ranking_card">
    <div class="d-flex justify-content-center center ranking_line">
        <h4 class="numbers" id="one">1.</h4>
        <div class="card">
            <div class="card-body ranking_slot" id="ranking1"></div>
        </div>
    </div>
</div>

<div class="travel_variables">
    <div class="d-flex justify-content-center center variable_row desktop" id="js_row">
        <div class="card variable climate" id="hot" onclick="click_variable(this.id)">
            <div class="card-body no_padding">
                Hot Climate
            </div>
        </div>

        <div class="card variable affordability" id="expensive" onclick="click_variable(this.id)">
            <div class="card-body no_padding">
                Expensive Trip
            </div>
        </div>
    </div>
</div>

js js

function click_variable(clicked_id) {

    if (document.getElementById(clicked_id).parentElement.id.includes("ranking")) {
        console.log(document.getElementById(clicked_id));
        row = document.getElementById("js_row");
        row.appendChild(document.getElementById(clicked_id));
    }

    if (document.getElementById("ranking1").firstChild == null) {
        document.getElementById("ranking1").appendChild(document.getElementById(clicked_id));

        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
            document.getElementById("ranking1").firstChild.style.height = '40px';
            document.getElementById("ranking1").firstChild.style.margin = '0px 0px 0px 0px';
            document.getElementById("ranking1").firstChild.style.marginTop = '-12px';
        }

        document.getElementById("ranking1").firstChild.style.margin = '0px 0px 0px 0px';
        document.getElementById("ranking1").firstChild.style.marginTop = '-12px';
        getValue();
    }
}

In the solution below, I made a toggle check to add and remove child <div> s from the parent <div> :在下面的解决方案中,我进行了切换检查以从父<div>添加和删除子<div> div> :

 let row = document.getElementById("js_row"); let ranking1 = document.getElementById("ranking1"); let toggleState = [false, false]; function getValue(){ } function add(element){ if (element.parentElement.id.includes("ranking")) row.appendChild(element); } function remove(element){ ranking1.appendChild(element); ranking1.firstChild.style.margin = '0px 0px 0px 0px'; ranking1.firstChild.style.marginTop = '-12px'; getValue(); } function click_variable(clicked_id) { let clicked_element = document.getElementById(clicked_id); if(clicked_id === "hot"){ if(toggleState[0]) add(clicked_element); else remove(clicked_element); toggleState[0] =;toggleState[0]; } else if(clicked_id === "expensive"){ if(toggleState[1]) add(clicked_element); else remove(clicked_element); toggleState[1] = !toggleState[1]; } }
 #ranking_card{ width: 100vw; border: 1px solid red; margin-bottom: 50px; }.travel_variables{ border: 1px solid blue; height: 150px; margin-top: 25px; } #js_row{ margin-top: 50px; }.card{ margin-top: 50px; }
 <div id="ranking_card"> <div class="d-flex justify-content-center center ranking_line"> <h4 class="numbers" id="one">1.</h4> <div class="card"> <div class="card-body ranking_slot" id="ranking1"></div> </div> </div> </div> <div class="travel_variables"> <div class="d-flex justify-content-center center variable_row desktop" id="js_row"> <div class="card variable climate" id="hot" onclick="click_variable(this.id)"> <div class="card-body no_padding"> Hot Climate </div> </div> <div class="card variable affordability" id="expensive" onclick="click_variable(this.id)"> <div class="card-body no_padding"> Expensive Trip </div> </div> </div> </div>

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

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