简体   繁体   English

使用Vanilla JS克隆具有特定属性的元素

[英]Clone an element with an specific attribute with Vanilla JS

I'm trying to clone an element with a specific attribute that I've set via JS. 我正在尝试克隆一个具有我通过JS设置的特定属性的元素。 The code: 编码:

HTML HTML

<div id="container">
  <div class="test" data-test></div>
  <div class="test" data-test data-cloneable></div>
  <div class="test" data-test></div>
</div>

<div>
  <button data-tool="clone">Clone</button>
</div>

One of those elements have an data-cloneable attribute that I'm checking with JS: 其中一个元素有一个data-cloneable属性,我正在用JS检查:

JavaScript JavaScript的

var container = document.getElementById("container");
var elements = document.querySelectorAll("[data-test]");

Array.prototype.forEach.call(elements, function(element) {

 cloneTool = document.querySelector("[data-tool='clone']");
 cloneTool.onclick = function() {
   var clone = element.cloneNode(true);
   if(element.hasAttribute("data-cloneable")) {
     container.appendChild(clone);
   }
 };

});

It's not working, ahh, more or less, the problem is that command only clones the last element in the container and not the specific element. 它不起作用,啊,或多或少,问题是命令只克隆容器中的最后一个元素而不是特定元素。

Something like this? 像这样的东西? Or do you mean that the clone would appear just under its origin? 或者你的意思是克隆会出现在它的起源之下?

http://jsfiddle.net/omikey/nupc7htn/2/ http://jsfiddle.net/omikey/nupc7htn/2/

var cloneTool = document.querySelector("div[data-cloneable='true']");
cloneTool.onclick = function(clone) {
  var node = this.cloneNode(true);
  var container = document.getElementById('container');
  container.appendChild(node);
};

<div id="container">
  <div class="test">1</div>
  <div class="test" data-cloneable="true">2</div>
  <div class="test">3</div>
</div>

One problem with your code is the event registration, since you are using onclick property in each iteration you are overriding the previously assigned click handler. 您的代码的一个问题是事件注册,因为您在每次迭代中使用onclick属性,而是覆盖先前分配的单击处理程序。

So at the end of the loop the only handler that is present is the one corresponding to the last data-test element, which when triggered does not have the data-clonable attribute so nothing happens. 因此,在循环结束时,唯一存在的处理程序是与最后一个data-test元素相对应的处理程序,当触发时它没有data-clonable属性,因此没有任何反应。

Do you mean something like 你的意思是什么?

 var container = document.getElementById('container'); var cloneTool = document.querySelector("[data-tool='clone']"); cloneTool.addEventListener('click', function() { var elements = document.querySelectorAll("[data-test][data-cloneable]"); Array.prototype.forEach.call(elements, function(element) { container.appendChild(element.cloneNode(true)); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="container"> <div class="test" data-test>1</div> <div class="test" data-test data-cloneable>2</div> <div class="test" data-test>3</div> </div> <div> <button data-tool="clone">Clone</button> </div> 

You are registering the onclick event multiple times. 您正多次注册onclick事件。 It only registers the last event you assigned, since the last element does not have the data-cloable attribute, then element.hasAttribute("data-cloneable") always returns false . 它只记录您指定的最后一个event ,因为最后一个元素没有data-cloable属性,所以element.hasAttribute("data-cloneable")总是返回false

var container = document.getElementById("container");

var cloneTool = document.querySelector("[data-tool='clone']");
cloneTool.onclick = function() {
    var elements = document.querySelectorAll("[data-test][data-cloneable]");
    Array.prototype.forEach.call(elements, function(element) {
        var clone = element.cloneNode(true);
        container.appendChild(clone);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM