简体   繁体   English

如何在javascript中现在检查新克隆的节点是否是最后一个节点

[英]How to check if newly cloned node is last node now in javascript

I am trying to clone the last row in a table. 我正在尝试克隆表的最后一行。 So when the last row is cloned, the new row obviously becomes the last node. 因此,当克隆最后一行时,新行显然成为最后一个节点。 How do I made it that when that is clicked, it, itself creates a new node 我如何做到这一点,当它被单击时,它本身会创建一个新节点

window.onload = function() {

    var table = document.getElementById("nodeTable");
    var lastRow = table.rows[ table.rows.length - 1 ];

    lastRow.addEventListener('click', function() {
        var clone = lastRow.cloneNode(true);
        // var row = table.insertRow(-1);
        table.appendChild(clone);
        // now set the newly cloned node as the last node
        lastRow = table.rows[ table.rows.length - 1 ];
    });

It seems to want to just leave it at the previous node. 似乎只想将其保留在上一个节点。 I hope this makes sense. 我希望这是有道理的。

cloneNode() does not clone event handlers . cloneNode()不克隆事件处理程序 So you should move the callback function to a separate variable/named function and register it every time you clone it. 因此,您应该将回调函数移至单独的变量/命名函数,并在每次克隆时进行注册。

Alternatively use event bubbling 或者使用事件冒泡

I'd suggest the following, listening for click-events at the level of the ancestor <table> and working out what element fired the event: 我建议采取以下措施,在祖先<table>级别侦听click-events,并找出触发该事件的元素:

var table = document.getElementById('nodeTable');

// binding the event-listener to the <table>:
table.addEventListener('click', function(e) {
  // this is the <table> itself,
  // this.rows is the collection of <tr> descendants:
  var rows = this.rows,
  // e.target is the clicked element:
    current = e.target,
    body = document.body,
  // getting the last row of the table:
    lastRow = rows[rows.length - 1];

  // while the clicked element does not have the (lower-cased) tagName of 'tr',
  // and it's not the 'body' (just for safety):
  while (current.tagName.toLowerCase() !== 'tr' && current !== body) {
    // we update current to be its own parentNode and go again
    current = current.parentNode;
  }

  // if the current-element is the lastRow:
  if (current === lastRow) {
    // we append the cloned row to the parentNode of the
    // lastRow (which will be a <tbody>, not the <table>:
    lastRow.parentNode.appendChild(lastRow.cloneNode(true));
  }
});

 var table = document.getElementById('nodeTable'); table.addEventListener('click', function(e) { var rows = this.rows, current = e.target, body = document.body, lastRow = rows[rows.length - 1]; while (current.tagName.toLowerCase() !== 'tr' && current !== body) { current = current.parentNode; } if (current === lastRow) { lastRow.parentNode.appendChild(lastRow.cloneNode(true)); } }); 
 table { counter-reset: rowCount; } td { border: 1px solid #000; width: 3em; height: 2em; } td::before { content: counter(rowCount); counter-increment: rowCount; } 
 <table id="nodeTable"> <tbody> <tr> <td></td> </tr> </tbody> </table> 

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

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