【问题标题】:I do not know how to keep it from cloning the delete button我不知道如何防止它克隆删除按钮
【发布时间】:2016-04-09 08:00:51
【问题描述】:

嗨,我已经为此工作了几个小时,但我仍然不明白我哪里出错了 这就是我们被要求做的事情

  1. 现在为每个新项目(不是第一个项目)添加一个删除按钮。点击这个按钮,对应的项目my-item,包括item-header和item-body,从页面中移除,其他不受影响

这就是我所做的

<!DOCTYPE HTML>
<html>
    <head>
    <title></title>
    <script src="library/jquery.js"></script>
    <script>
    /*This task illustrates the sliding effects that can be produced
    with jQuery. Create a div with the class my-item that contains two 
    other divs, the first with the class item-header and the second
    with the class item-body. The item-header div contains an <h2> tag
    with some text (e.g. Click Me). The item-body div also contains some 
    text, but the div is initially hidden. If the user clicks on the 
    <h2> headline, the div item-body should slide down and its contents 
    become visible. After another click on the headline, the  div should 
    slide up and its contents become invisible

    Extend your code from the previous task in such a way that a deep 
    copy of the first div with the class my-item can be appended to the 
    body of the document by clicking on a button. Be sure to modify your 
    event handler from the previous task so that it also works for the 
    new elements.

    Now add a button Delete to each new item (not to the first one). By 
    clicking on this button, the corresponding item my-item, including 
    item-header and item-body, is removed from the page, while the 
    others are not affected.*/

        $(document).ready(function() {

        $('#copy').click(function(){
        $('.my-item:last')//i have to exclude the first 1
        .clone()
        .append( '<button id = "away">Delete</button>' )
        .appendTo('body')
        ;
        });

       $('.item-body').hide();

            $(document).on( 'click','h2', function() {

            if($(this).parent().next().is(':visible')){

            $(this).parent().next().slideUp();

            }

            else{

            $(this).parent().next().slideDown();

            }



        $(document).on('click', '#away', function() {
        $(this).parent().remove();

        });   




        });













        });


    </script>
    </head>
        <body>
        <div class = 'my-item'>
            <div class = 'item-header'>
                <h2>Jesus is the only Hope</h2>
            </div><!--closing tag for for div item-header-->

            <div class = 'item-body'>
                <h2>I believe in Him</h2>
                <p>je leve les yeux vers les monts que jaime dou peut me venir ici le secours. Le secours me vient de lEternel meme</p>
            </div><!--closing tag for the div item-body-->

       </div><!-- This is the closing tag of div my_item-->
       <button id ='copy'>copy</button>

        </body>
</html>

请任何人给我任何提示,以便我知道我哪里出错了?

【问题讨论】:

  • 一个问题是您创建了重复的 ID。

标签: jquery clone infinite


【解决方案1】:

您已将delete 按钮的点击事件置于h2 的点击事件中。将它放在h2 点击事件之外和$(documant).ready() 中。克隆 $('.my-item:first') 而不是 $('.my-item:last'),因为从第二个 $('.my-item:last') 开始,它已经被删除了。

<html>
    <head>
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#copy').click(function () {
                    $('.my-item:first')
                    .clone()
                    .append('<button id = "away">Delete</button>')
                    .appendTo('body')
                    ;
                });

                $('.item-body').hide();

                $(document).on('click', 'h2', function () {
                    if ($(this).parent().next().is(':visible')) {
                        $(this).parent().next().slideUp();
                    }
                    else {
                        $(this).parent().next().slideDown();
                    }
                });

                $(document).on('click', '#away', function () {
                    $(this).parent().remove();
                });
            });
        </script>
    </head>
    <body>
        <div class='my-item'>
            <div class='item-header'>
                <h2>Jesus is the only Hope</h2>
            </div><!--closing tag for for div item-header-->

            <div class='item-body'>
                <h2>I believe in Him</h2>
                <p>je leve les yeux vers les monts que jaime dou peut me venir ici le secours. Le secours me vient de lEternel meme</p>
            </div><!--closing tag for the div item-body-->

        </div><!-- This is the closing tag of div my_item-->
        <button id='copy'>copy</button>

    </body>
</html>

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2015-12-31
    • 1970-01-01
    相关资源
    最近更新 更多