如何在使用jquery中的类删除元素时继承事件处理程序?

时间:2017-04-11 17:18:00

标签: javascript jquery html

我的html如下

    <div id="parent_div">
        <div class="child_div">
            <p>clicked button 1</p>
            <button id="button1">button1</button>
        </div>

        <div class="child_div">
            <p>clicked button 2</p>
            <button id="button2">button2</button>
        </div>
    </div>

    <button id="remover">remove</button>

    <script type="text/javascript">
        $('#button1').click(function(event) {
            $(this).prev().css('color','red');
        });

        $('#button2').click(function(event) {
            $(this).prev().css('color','yellow');
        });

        var new_html = '<div class="child_div">\
                            <p>new button 1</p>\
                            <button id="button1">button1</button>\
                        </div>\
                        <div class="child_div">\
                            <p>new button 2</p>\
                            <button id="button2">button2</button>\
                        </div>\
                        <div class="child_div">\
                            <p>new button 3</p>\
                            <button id="button3">button3</button>\
                        </div>'

        $('#remover').click(function(event) {
            $('#parent_div').children().detach();
            $('#parent_div').append(new_html);
        });
</script>

在上面的代码中,当单击卸妆按钮时,我希望 parent_div 变为空,但我需要保留放在它们上面的事件处理程序。因为稍后,我添加了不同的html,但是我之前放置了相同的ID。当我尝试使用上面的代码执行此操作时,无论 detach()或remove()或emtpy()方法如何,我的事件都会被隐藏。在新元素上,我的点击事件无效。请帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

检查此工作代码。

   

 $().ready(function(){
       $('#parent_div').on('click', '#button1', function (event) {
            $(this).prev().css('color', 'red');
        });
        $('#parent_div').on('click', '#button2', function (event) {
            $(this).prev().css('color', 'yellow');
        });
        $('#parent_div').on('click', '#button3', function (event) {
            $(this).prev().css('color', 'green');
        });

        var new_html = '<div class="child_div">\
                        <p>new button 1</p>\
                        <button id="button1">button1</button>\
                    </div>\
                    <div class="child_div">\
                        <p>new button 2</p>\
                        <button id="button2">button2</button>\
                    </div>\
                    <div class="child_div">\
                        <p>new button 3</p>\
                        <button id="button3">button3</button>\
                    </div>'

        $('#remover').click(function (event) {
            $('#parent_div').children().detach();
            $('#parent_div').append(new_html);
        });
    });
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<div id="parent_div">
    <div class="child_div">
        <p>clicked button 1</p>
        <button id="button1">button1</button>
    </div>

    <div class="child_div">
        <p>clicked button 2</p>
        <button id="button2">button2</button>
    </div>
</div>

<button id="remover">remove</button>