2015-07-19 65 views
-1

我只是想克隆和插入这个克隆的右手边的按钮。如何克隆div和插入按钮?

我的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>"); 
      } 

     } 
    }); 

我希望它是这样的:

enter image description here

+0

请提供给我们多一点的代码(HTML),甚至一个的jsfiddle链接,以便我们可以帮助你更好;) – nstungcom

+0

我认为你可以做到这一点很简单,我建议你,不要混用'纯js'和'jQuery'。 jsfiddle链接将会有用 –

+0

'clone(document.getElementById(this.id)=== clone(this)' – charlietfl

回答

0

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

变化:

$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