简体   繁体   English

如何“删除”包含所有子元素的元素并将其“放置”在另一个 div 中,同时保留所有 jQuery 功能?

[英]How to "remove" element with all children inside and "place" it in another div while keeping all jQuery functionality?

First: It is difficult to post a example of the JavaScript and the HTML from the page because I could not get it to work in JSfiddle.第一:很难从页面中发布 JavaScript 和 HTML 的示例,因为我无法让它在 JSfiddle 中工作。

I have a page and with jQuery I simulate that there are two tabs.我有一个页面,并使用 jQuery 我模拟有两个选项卡。 It is basically the same container but the content that is for the "second" tab is hidden.它基本上是相同的容器,但隐藏了“第二个”选项卡的内容。 When you click on a element in the tabs it should fold open.当您单击选项卡中的元素时,它应该折叠打开。 It opens, but when you open the element the next element to the right goes down the open element.它会打开,但是当您打开元素时,右侧的下一个元素会沿着打开的元素向下移动。 This leaves a big gap on the right because there is a hidden element there.这在右边留下了一个很大的空白,因为那里有一个隐藏的元素。 How can I get the hidden elements and "remove" them from the DOM and then when I click on the second tab "paste" them there?如何获取隐藏元素并从 DOM 中“删除”它们,然后当我单击第二个选项卡时将它们“粘贴”到那里?

I have tried it with remove(), detach() and hide() but it does not work.我已经尝试过使用 remove()、detach() 和 hide() 但它不起作用。 With detach() it removes the hidden elements form the DOM, but does not paste them in the other tab.使用 detach() 它从 DOM 中删除隐藏元素,但不会将它们粘贴到另一个选项卡中。

Here is a example of my code:这是我的代码示例:

function fixMyVouchers() {
var myVouchers = $("#my-vouchers");
var handedIn = $("#handed-in");
var handedInDeal = $(".handedIn");
var myVouchersContainer = $(".my-vouchers-container");

if(myVouchers.hasClass("active")) {
    handedInDeal.each(function() {
        $(this).detach();
        console.log("Removed " + handedInDeal.length + " handed in deals");
    });
}

handedIn.on("click", function() {
    myVouchersContainer.html(handedInDeal);
    console.log("click");
});
}

And here is the HTML Body Code:这是 HTML 正文代码:

<div class="row my-vouchers-container" id="myVouchers">
    <div class="mix col-xs-4 notExpired" data-my-enddate="88" data-my-purchase="0" data-hash="1-of-3-behandelingen-brazillian-wax-en-harsen-van-onderbenen-en-oksels-bij-ayurveda-beauty-spa-veda-lakshmi-utrecht" data-href="http://local.socialdeal.nl/voucher/info/64l50c6dg4heiy7yn4/" style="display: inline-block;">
        <div class="social-deal social-deal-etc">
            <div class="social-deal-image">
                <div class="social-deal-image-wrap">
                    <div class="social-deal-voucher-info"></div>
                    <img src="https://media.socialdeal.nl/deal/ayurveda-beauty-en-spa-veda-lakshmi-15062513000919.jpg" alt="Brazilian wax + harsen bij Ayurveda" title="Brazilian wax + harsen bij Ayurveda" class="sd-image-full" />
                </div>
                <div class="social-deal-diamond"> <i class="icon icon-category-beauty cat-beauty-color"></i> 
                </div>
            </div>
            <div class="social-deal-bottom">
                <div class="social-deal-info">
                    <h4 class="social-deal-title">Brazilian wax + harsen bij Ayurveda</h4>

                    <div class="social-deal-company">
                        <div class="social-deal-company-name">Ayurveda Beauty &amp; Spa | Veda Lakshmi</div>
                        <div class="social-deal-company-location dottedIfLong"> <i class="icon-pointer3"></i>
                            Utrecht</div>
                    </div>
                </div>
                <div class="voucher-statistics"> <span class="voucher-info">
                    Maanden resterend: 2                    <br/>
                    Vouchers: 1                </span>

                </div>
            </div>
        </div>
    </div>
    <div class="mix col-xs-4 notExpired" data-my-enddate="87" data-my-purchase="0" data-hash="authentiek-italiaans-3-gangen-keuzediner-bij-ristorante-brunos-in-hartje-utrecht-met-bij-het-hoofdgerecht-keuze-uit-alle-pastas-van-de-kaart-utrecht" data-href="http://local.socialdeal.nl/voucher/info/mqywycdt5t5n56y/" style="display: inline-block;">
        <div class="social-deal social-deal-etc">
            <div class="social-deal-image">
                <div class="social-deal-image-wrap">
                    <div class="social-deal-voucher-info">
                        <div class="social-deal-info-item">
                            <div class="social-deal-no-reservation" title="" data-toggle="tooltip" data-placement="left" data-original-title="Maak een reservering"></div>
                        </div>
                    </div>
                    <img src="https://media.socialdeal.nl/deal/ristorante-brunos-15062412531317.jpg" alt="Italiaans 3-gangendiner (25 keuzes)" title="Italiaans 3-gangendiner (25 keuzes)" class="sd-image-full" />
                </div>
                <div class="social-deal-diamond"> <i class="icon icon-category-food cat-food-color"></i> 
                </div>
            </div>
            <div class="social-deal-bottom">
                <div class="social-deal-info">
                    <h4 class="social-deal-title"> Italiaans 3-gangendiner (25 keuzes)</h4>

                    <div class="social-deal-company">
                        <div class="social-deal-company-name">Ristorante Bruno´s</div>
                        <div class="social-deal-company-location dottedIfLong"> <i class="icon-pointer3"></i>
                            Utrecht</div>
                    </div>
                </div>
                <div class="voucher-statistics"> <span class="voucher-info">
                    Maanden resterend: 2                    <br/>
                    Vouchers: 1                </span>

                </div>
            </div>
        </div>
    </div>
    <div class="mix col-xs-4 notExpired" data-my-enddate="79" data-my-purchase="0" data-hash="waardebon-twv-15-euro-te-besteden-op-het-gehele-assortiment-van-cacao-ambachtelijke-en-huisgemaakte-chocolade-en-bonbons-utrecht" data-href="http://local.socialdeal.nl/voucher/info/lg3j7cntmt969jw/" style="display: inline-block;">
        <div class="social-deal social-deal-etc">
            <div class="social-deal-image">
                <div class="social-deal-image-wrap">
                    <div class="social-deal-voucher-info"></div>
                    <img src="https://media.socialdeal.nl/deal/cacao-15061611000264.jpg" alt="Ambachtelijke chocolade en bonbons" title="Ambachtelijke chocolade en bonbons" class="sd-image-full" />
                </div>
                <div class="social-deal-diamond"> <i class="icon icon-category-store cat-store-color"></i> 
                </div>
            </div>
            <div class="social-deal-bottom">
                <div class="social-deal-info">
                    <h4 class="social-deal-title">Ambachtelijke chocolade en bonbons</h4>

                    <div class="social-deal-company">
                        <div class="social-deal-company-name">Cacao</div>
                        <div class="social-deal-company-location dottedIfLong"> <i class="icon-pointer3"></i>
                            Utrecht</div>
                    </div>
                </div>
                <div class="voucher-statistics"> <span class="voucher-info">
                    Maanden resterend: 2                    <br/>
                    Vouchers: 1                </span>

                </div>
            </div>
        </div>
    </div>
    <div class="mix col-xs-4 notExpired" data-my-enddate="77" data-my-purchase="0" data-hash="om-af-te-halen-bij-new-york-pizza-een-pizza-25-cm-naar-keuze-een-blikje-cola-of-fanta-perfect-als-je-even-geen-zin-hebt-om-te-koken-utrecht" data-href="http://local.socialdeal.nl/voucher/info/y0on0ietkt161j5/" style="display: inline-block;">
        <div class="social-deal social-deal-etc">
            <div class="social-deal-image">
                <div class="social-deal-image-wrap">
                    <div class="social-deal-voucher-info"></div>
                    <img src="https://media.socialdeal.nl/deal/new-york-pizza-utrecht-15061400070257.jpg" alt="New York Pizza + blikje cola of fanta" title="New York Pizza + blikje cola of fanta" class="sd-image-full" />
                </div>
                <div class="social-deal-diamond"> <i class="icon icon-category-food cat-food-color"></i> 
                </div>
            </div>
            <div class="social-deal-bottom">
                <div class="social-deal-info">
                    <h4 class="social-deal-title">New York Pizza + blikje cola of fanta</h4>

                    <div class="social-deal-company">
                        <div class="social-deal-company-name">New York Pizza Utrecht</div>
                        <div class="social-deal-company-location dottedIfLong"> <i class="icon-pointer3"></i>
                            Utrecht</div>
                    </div>
                </div>
                <div class="voucher-statistics"> <span class="voucher-info">
                    Maanden resterend: 2                    <br/>
                    Vouchers: 1                </span>

                </div>
            </div>
        </div>
    </div>
    <div class="mix col-xs-4 notExpired" data-my-enddate="60" data-my-purchase="0" data-hash="culinaire-workshop-lunchgerechten-maken-2-5-uur-of-een-workshop-summertapas-3-5-uur-of-een-van-gogh-schilderworkshop-6-5-uur-bij-grachtenatelier-utrecht" data-href="http://local.socialdeal.nl/voucher/info/x64exi3wv1c4iqwq6g/" style="display: inline-block;">
        <div class="social-deal social-deal-etc">
            <div class="social-deal-image">
                <div class="social-deal-image-wrap">
                    <div class="social-deal-voucher-info"></div>
                    <img src="https://media.socialdeal.nl/deal/het-grachtenatelier-15062316390867.jpg" alt="Culinaire workshop of schilderworkshop" title="Culinaire workshop of schilderworkshop" class="sd-image-full" />
                </div>
                <div class="social-deal-diamond"> <i class="icon icon-category-course cat-course-color"></i> 
                </div>
            </div>
            <div class="social-deal-bottom">
                <div class="social-deal-info">
                    <h4 class="social-deal-title">Culinaire workshop of schilderworkshop</h4>

                    <div class="social-deal-company">
                        <div class="social-deal-company-name">Het GrachtenAtelier</div>
                        <div class="social-deal-company-location dottedIfLong"> <i class="icon-pointer3"></i>
                            Utrecht</div>
                    </div>
                </div>
                <div class="voucher-statistics"> <span class="voucher-info">
                    Dagen resterend: 60                    <br/>
                    Vouchers: 1                </span>

                </div>
            </div>
        </div>
    </div>
    <div class="mix col-xs-4 notExpired" data-my-enddate="54" data-my-purchase="0" data-hash="gehele-dag-entree-met-gebruik-van-alle-saunafaciliteiten-bij-sauna-soesterberg-geniet-van-pure-rust-en-ontspanning-juni-2015-amersfoort-utrecht" data-href="http://local.socialdeal.nl/voucher/info/o37dkidt1t494v7/" style="display: inline-block;">
        <div class="social-deal social-deal-etc">
            <div class="social-deal-image">
                <div class="social-deal-image-wrap">
                    <div class="social-deal-voucher-info"></div>
                    <img src="https://media.socialdeal.nl/deal/sauna-soesterberg-15062612501019.jpg" alt="Bij Sauna Soesterberg een gehele dag sauna " title="Bij Sauna Soesterberg een gehele dag sauna " class="sd-image-full" />
                </div>
                <div class="social-deal-diamond"> <i class="icon icon-category-wellness cat-wellness-color"></i> 
                </div>
            </div>
            <div class="social-deal-bottom">
                <div class="social-deal-info">
                    <h4 class="social-deal-title">Bij Sauna Soesterberg een gehele dag sauna </h4>

                    <div class="social-deal-company">
                        <div class="social-deal-company-name">Sauna Soesterberg</div>
                        <div class="social-deal-company-location dottedIfLong"> <i class="icon-pointer3"></i>
                            Soesterberg</div>
                    </div>
                </div>
                <div class="voucher-statistics"> <span class="voucher-info">
                    Dagen resterend: 54                    <br/>
                    Vouchers: 4                </span>

                </div>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
</div>

The problem is that the handedIn elements are hidden.问题是 handedIn 元素被隐藏了。 And are ruining the layout.并且正在破坏布局。 I know how to remove them from the DOM but not how to paste them in the other tab.我知道如何从DOM删除它们,但不知道如何将它们粘贴到另一个选项卡中。

Any help would be appreciated.任何帮助,将不胜感激。

Well, you have a lot of code.好吧,你有很多代码。 What I could do is, I can replicate something quick and dirty to match your question title.我能做的是,我可以复制一些快速而肮脏的东西来匹配你的问题标题。 Check out the snippet.查看片段。 The following snippet has two divs.以下代码段有两个 div。 One is an empty one, and the other has some elements with event handler attached to it.一个是空的,另一个有一些附加了事件处理程序的元素。 I understand that you need to move them without changing the event handlers.我知道您需要在不更改事件处理程序的情况下移动它们。

 $(function () { $("#one a").click(function () { alert("Hi"); return false; }); a = $("#one").clone(true, true); a.appendTo("#two"); $("#one").html(""); });
 #one a {color: blue;} #two a {color: red;}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="one"> <a href="#" id="one-a">One A</a> </div> <div id="two"></div>

Test:测试:

  1. The colour should turn red.颜色应该变成红色。
  2. Clicking on One should get an alert Hi .单击 One 应该会收到警报Hi

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

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