移动div而不是Remove()

时间:2012-07-20 22:41:17

标签: javascript jquery

        $(settings.widgetSelector, $(settings.columns)).each(function () {
        var thisWidgetSettings = iNettuts.getWidgetSettings(this.id);
        if (thisWidgetSettings.removable) {
            $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
                /* STOP event bubbling */
                e.stopPropagation();    
            }).click(function () {
                if(confirm('This widget will be removed, ok?')) {
                    $(this).parents(settings.widgetSelector).animate({
                        opacity: 0    
                    },function () {
                        $(this).wrap('<div/>').parent().slideUp(function () {
                            $(this).remove();
                            iNettuts.savePreferences();
                        });
                    });
                }
                return false;
            }).appendTo($(settings.handleSelector, this));
        }

现在基本上这个代码在单击“关闭”时会完全删除内容。我宁愿做的是把它移到另一个div。我正在读关于prependTo。我认为这就像改变一样简单:

$(this).remove();

要:

$(this).prependTo('.dock');

但似乎并不那么简单。这只是删除它。 Full Code

2 个答案:

答案 0 :(得分:1)

尝试jQuery detach,因为删除也会删除所有元素的引用。

    $(settings.widgetSelector, $(settings.columns)).each(function () {
    var thisWidgetSettings = iNettuts.getWidgetSettings(this.id);
    if (thisWidgetSettings.removable) {
        $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
            /* STOP event bubbling */
            e.stopPropagation();    
        }).click(function () {
            if(confirm('This widget will be removed, ok?')) {
                $(this).parents(settings.widgetSelector).animate({
                    opacity: 0    
                },function () {
                    $(this).wrap('<div/>').parent().slideUp(function () {
                        $(this).detach().prependTo('.dock');
                        iNettuts.savePreferences();
                    });
                });
            }
            return false;
        }).appendTo($(settings.handleSelector, this));
    }

答案 1 :(得分:1)

这将保留原始元素:

$('yourElement').clone().appendTo('whereverYouWant');

<强> jsBin demo

<小时/> 如果您想将其从原始位置删除:

$('yourElement').appendTo('whereverYouWant');

<强> jsBin demo

同样:

$('yourElement').remove().clone().appendTo('whereverYouWant');