如何同时克隆 div 和插入按钮?

How to clone div and insert button together?

我只想在这个克隆按钮的右手边克隆并插入按钮。

我的 javascript 看起来像这样:

 $('.frame .portlet').click(function (e) {


        if ($(e.target).is(".portlet-content") || $(e.target).is(".portlet-header")) {
            cancel: 'ui-icon-minusthick ui-icon-plusthick';
            var $this = $(this), $error;

            // for duplicate
            if ($this.hasClass('added') && !$this.hasClass('newPortlet')) {
                $error = $this.next('.clone-error');
                if (!$error.length) {
                    $error = $('<span />', {
                        'class': 'clone-error',
                        html: '<div class="alert alert-warning" role="alert" style="width: 300px"> This question is already added!</div>'
                    }).insertAfter(this);
                }
                $error.stop().show().delay(800).hide(1);
            } else if (!$this.hasClass('newPortlet')) {
                $this.addClass('added').clone(document.getElementById(this.id)).addClass('newPortlet').("<button>hi</button>").appendTo('#creation .newFrame');

                $msg = $('<span />', { html: '<div class="alert alert-success" role="alert" style="width: 300px"> This question is added to Add List!</div>' }).insertAfter(this);
                $msg.stop().show().delay(800).hide(1);
                count++;
                add.html("<span id='newExam' class='badge' style='background-color: lightgreen; border-color: green'>" + count + "</span>");
            }

        }
    });

我希望它是这样的:

您可以存储对克隆对象的引用....然后通过附加到该对象来操作该对象。

变化:

$this.addClass('added').clone(document.getElementById(this.id))
      .addClass('newPortlet').appendTo('#creation .newFrame');

收件人:

  // create clone object
  var $clone = $this.addClass('added').clone(this);
  // now can append to clone
  $clone.append('<button>TEST</button>');
  // then insert clone to dom          
  $clone.addClass('newPortlet').appendTo('#creation .newFrame');

通过将对象创建为变量,可以更轻松地阅读和查看各个步骤。我认为您的主要问题只是链条中有许多步骤才能使逻辑易于遵循

DEMO