JQuery删除无法正常工作

时间:2017-02-20 21:33:40

标签: jquery html jquery-mobile

我遇到了$('element').remove();不起作用的问题。 我已经阅读了许多其他答案,其中存在语法问题,并且未正确地在按钮上注册了单击。

我遇到了另一个问题。点击工作正常,但唯一删除的是 newRowDiv

它开始<newRowDiv><child><child><child></newRowDiv> 并结束 <child><child><child> newRowDiv 已经消失,但孩子们只是离开了那里。 奇怪的是代码工作正常,直到我添加trigger('create')

这是我的代码所做的事情

buildRows = function(){
            //Adds the new div to the placement div
            var placementDiv = $('#placementDiv');
            placementDiv.append("<div class='newRowDiv'></div>");
            //Adds the child elements to the new div
            var newRowDiv = $(".newRowDiv:last");
            newRowDiv.append("<label>Title</label>");
            newRowDiv.append("<input>");
            newRowDiv.append("<a id='plusButton'>Plus</a>");

            //Adds the click event to the new plus button (Works fine)
            newRowDiv.find('#plusButton').click(function (event) {
                var button = $(event.target);
                //Adds the delete button (Works fine)
                var newRowDiv = button.parent('.newRowDiv');
                newRowDiv.append('<a id="deleteButton">Delete</a>');
                //Adds the click event to the button (Works fine)
                newRowDiv.find('#deleteButton').click(function (event) {
                    //Removes the parent newRowDiv and all child elements (Doesn't work)
                    var button = $(event.target);
                    button.closest('.newRowDiv').remove();
                });

                //Removes the plus button (Works fine)
                button.remove();

                //Adds the new rows section (Works fine)
                buildRows();
                //Without this the new elements don't display correctly
                //With this the remove only removes the element and leaves it's children behind
                placementDiv.trigger('create');
            });
        }
buildRows()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="placementDiv">
    </div>

编辑: 代码段正在运行。当我运行触发器时,我只在我的东西上遇到问题。('create')这是添加一些jquery移动格式

编辑2:它似乎与jquery mobile的弹出格式有关。因为它在没有弹出窗口的代码片段中工作正常,并且在我添加触发器('create')之前它在原始代码中工作正常

1 个答案:

答案 0 :(得分:-2)

尝试使用.delegate()而不是.click()。

相关问题