clone()不绑定事件/插件

时间:2017-11-02 03:18:54

标签: javascript jquery html css clone

我有视差附加到我克隆的同一个div但是在我克隆视差后停止为克隆元素工作。
查看我的示例:https://codepen.io/anon/pen/rYOrEO

我的代码: Html:

<div class="container">
  <a href="#" class="clone">Clone</a>
</div>
<section class="content">
  <div class="jumbotron text-center bg-faded my-5" style="background: url('http://lorempixel.com/1200/600/abstract/1') no-repeat center;" data-paroller-factor="0.5">
    <h1 class="heading-1 py-5 text-white">Hello Parallax!</h1>
  </div>
</section>

JavaScript:

$(window).paroller();
$(function(){
  $('.clone').on('click', function() {
    alert('hi');
    $(".jumbotron").clone().appendTo(".content");
  });
});

示例:https://codepen.io/anon/pen/rYOrEO

提前完成

1 个答案:

答案 0 :(得分:0)

之前我遇到过类似的问题,可以做到。这是一个问题,CSS在页面生效时生效,而不是在你克隆时生效。

如果我记得修复它的权利,你需要创建克隆div的对象,类似于这个例子:

// will be different for you!! but might set you on the right direction
var divToClone = document.getElementsByID("#cloneMe");
var clone = divToClone.cloneNode(true);
var element = clone.getElementsByTagName("h1"); 

现在使用我的var元素,我可以像这样再次添加属性和样式

//Nothing to do with parallax here but look at you CSS and fill it in again like below
element.innerHTML = "Whatever you want or need"; 
element.style['background']= "url(/iwa/images/16x16/randomExample.png) no-repeat, fixed";
element.style['background-position']='center right';
element.style['border']='none'; 

希望有所帮助!