【问题标题】:Move an element from div to anther - not working将一个元素从 div 移动到另一个 - 不起作用
【发布时间】:2013-08-25 05:33:13
【问题描述】:

我正在尝试将 Grid3 移动到 Grid2 - 我正在使用此代码,但它不起作用,我做错了什么?

还有没有办法将 Grid3 中的一半 Div 移动到 Grid1,而将另一半移动到 Grid2?

JS

$("#grid3").appendTo("#grid2");

HTML

        <div class="wrapperA1">
            <div class="content">
                <div id="home-sectionD">
                    <div id="grid1"><!--Gird 1-->   

                        <article class="testimonial">
                        <img alt="Neal Kilburne​​" src="assets/images/neal kilburne​​.jpg"/>
                            <div>
                                <h3>Neal Kilburne​​</h3>
                                <p>CEO, iTEQ Global www.iteqglobal.com</p>
                                    <br>
                                <p>“Loai is a great asset to our company and provides us with great and quick responses,Such a talented designer which we have the honour of working with.” 2011 - 2012</p>
                            </div>  
                        </article>

                        <article class="testimonial">
                            <div>
                                <h3>Amanda Chui​</h3>
                                <p>Owner of www.beautyroom.ca</p>
                                    <br>
                                <p>Just what my website needed! When I had finished my website, I felt that it was missing something,so I enlisted in the help of Loai. He did a great job of giving my website the professional and polished look it needed without having me wait for days on end. Thanks, Loai!” June 23, 2012</p>
                            </div>
                        </article>

                    </div><div id="grid2"><!--Gird 2-->     

                        <article class="testimonial">
                            <img alt="Geeta Martinez" src="assets/images/geeta martinez.jpg"/>
                            <div>
                                <h3>Geeta Martinez</h3>
                                <p>Lawyer &amp; Business Consultant</p>
                                    <br>
                                <p>"Leo did a great job! He designed and put together several websites for me in less than a week. He was incredibly patient and flexible throughout the whole process, and took a lot of the stress out of the whole situation for me. He is a really nice guy to work with - I really appreciated his approach! I would definitely recommend working with him". July 14, 2013</p>
                            </div>  
                        </article>

                        <article class="testimonial">
                            <div>
                                <h3>Richard Jackson</h3>
                                <p><em>Photographer www.rjpstudios.co.uk​</em></p>
                                    <br>
                                <p>“Loai designed my website last year on wix though I could have done it myself loai added a proffesional touch to the design which is so important in creating the best first impeson.” 2013</p>
                            </div>
                        </article>  

                    </div><div id="grid3"><!--Gird 3-->         

                        <article class="testimonial">
                            <img alt="Glen Eric Huysamer" src="assets/images/glen eric huysamer.jpg"/>
                            <div>
                                <h3>Glen Eric Huysamer​</h3>
                                <p>Specialist Service Provider.</p>
                                    <br>
                                <p>“I would like to take this opportunity to warn people who might consider using Loai Design Studio. You will have to buckle up and strap yourself in as this young designer and associates take you through the process of creating your design needs. I was pleasantly surprised from start to finish, and can say that even though Loai took control of the creative process the end result felt like it was mine. You can not go wrong with this young lad, go ahead surprise yourself”. December 30, 2011</p>
                            </div>
                        </article>

                        <article class="testimonial">
                            <div>
                                <h3>Ciprian Filip​</h3>
                                <p>Founder of Pontomat.ro​</p>
                                    <br>
                                <p>“Worked with Loai on designing exposure of our social media presence on Facebook for our E-commerce initiative. He is very passionate and expert in his field of work, coming with breakthrough innovations in real time. He is able to manage an end to end social media exposure with accent on clarity, effectiveness and innovation. His working capabilities are awesome and I am sure that he will make good contribution to any project he works on.”  August 4, 2011</p>
                            </div>
                        </article>

                    </div>                      
                </div>
            </div>
        </div>

【问题讨论】:

  • 你从哪里调用 jQuery 代码?在 HTML 之前?是否包含在$(function(){ ... }); 中?是在举办某种活动吗?
  • 此处信息不足,无法提供答案。该附加是由事件触发的吗?您确定该事件被正确触发了吗?
  • 修复了问题,抱歉信息湖,我试图让它在屏幕尺寸低于 770px 时附加它...我正在使用以下代码,但它不能正常工作! $(document).load($(window).bind("resize", listenWidth)); function listenWidth() { if($(window).width()&lt;770) $('#grid3').find('article').appendTo('#grid2'); };

标签: javascript jquery html move


【解决方案1】:

似乎对我有用 (http://jsfiddle.net/k2NMD/9/)。

在浏览器实际处理所有 html 之前,您可能过早调用 append。尝试将您的附加函数包装在 $(document).ready() 函数中。

$(document).ready(function () {
    $("#grid3").appendTo("#grid2");
});

【讨论】:

  • 这有效,问题似乎是在内容尚未准备好时触发该功能。有没有办法将 Grid3 内的 Div 的一半附加到 Grid1 和 Grid2?
  • 当然。查看this link。这是假设您的一半内容在
    内。如果不是,您将不得不将一半的内容包装在一个 div 中并给它一个不同的类。然后用javascript代码中的新类名替换“testimonial”。
  • 这行得通,但是如果假设我在 Grid3 中有 4 篇文章,则只有前两篇将移至 Grid1 和 Grid2,其余 2 篇将保留在 Grid3 下,Java 无法解决而是将每一半放在一个div中?感谢您的帮助!
  • 完美运行!非常感谢!
【解决方案2】:

移动确实有效(如果您在文档加载后运行代码,例如在$(document).ready() 事件中)。在开发者控制台中检查元素(如果您使用的是 Chrome)以查看 #grid3 实际上在 #grid2 中,就像这样

<div id="grid2">...
    <article...>
    <article...>
    <div id="grid3">...
</div>

如果您想将grid3 的内容移动到grid2 中,那么您必须使用$('#grid3').find('article').appendTo('#grid2') 获取文章,这将从grid3 中删除文章并将其插入grid2。它的移动部分可以通过分离article并将一个插入grid1和另一个插入grid2来完成。

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多