因此,在Javascript中,我只能复制一次HTMl Id="characters"包装.我知道从技术上讲,它应该是一个"类",而不是一个"Id",因为它将是一个重复的"Id",但出于某种原因,我没有得到;当我将"getElementById"更改为"getElementsByClassName",将HTML"Id"更改为"class"时,它根本不会重复.也因为我在使用克隆. node (true),我正在失go 复制包装中"addEventListeners"的功能.有没有办法只用普通的Javascript来纠正这个问题?似乎这还不够烦人,我的复制包装器似乎已经从CSS网格中消失了.这一切都非常乏味和麻烦,所以我感谢你给我的任何建议.

这是我目前的HTML.

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/style.css">
        <h1><!--D&D DM Tool--><h1>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title></title>
    </head>
    <body>
        <div id="header-container">
            <h1 id="header">Game Tools</h1>
        </div>

        <div class="importantbuttons">
            <button id="resetbutton">Next Round</button>
            <button id="orderbutton">Order</button>
            <button id="zerobutton">Zero</button>
            <button id="deadremoverbtn">Bring Out the Dead!</button>
            <button id="addnpcbtn">Add NPC</button>
        </div>

        <div id="grid"> 
            <div class="characters">
                <div id="subgrid" class="subgrid" >
                    <div class="name-bloodiedstuff">
                        <div class="halfWayDown"></div>
                        <div class="character-name-display">Name</div>
                        <button class="name-submit-btn">Submit</button>
                        <input type="text" class="input-name-el" placeholder="Name">
                        <div class=int-stuf>
                            <div class="roll-display">Iniative</div>
                            <button class="iniativebtn">Submit</button>
                            <input type="number" class="iniative-roll-el" placeholder="Iniative Roll">
                        </div>
                        <div class="healthpoints-display">Healthpoints</div>
                        <button class="hp-submit-btn">Submit</button>
                        <input type="number" class="input-hp-el" placeholder="Total HealthPoints">
                        <button class="hp-deductin-btn">Submit</button>
                        <input type="number" class="input-hp-deduction-el" placeholder="Damage">
                    </div>
                    <div class="weapons-display">Weapons</div>
                    <button class="weapon-btn">Submit</button>
                    <input type="text" class="weapon-input-el" placeholder="Weapons">
                    <button class="active-btn" class="button">Active</button>       
                </div>
            </div>
        </div>
    </body>
    <script src="mainCopy.js"></script>
</html>

下面是我当前的Javascript复制功能.

addNpcBtn.addEventListener("click",function(){
    var characterSubGrids=document.getElementsByClassName("characters");
    console.log(characterSubGrids[0]);
    var characterSubGridsClone=characterSubGrids[0].cloneNode(true);
    let grid=document.getElementById("grid");
    console.log(grid);
    grid.appendChild(characterSubGridsClone);
});

推荐答案

From the MDN article on cloneNode

克隆 node 会复制其所有属性及其值,包括内在(内联)侦听器.它不会复制使用addEventListener()添加的事件监听器或分配给元素属性的事件监听器(例如node.onclick=someFunction).

似乎cloneNode可能已经忽略了您试图忽略的事件侦听器.

如果你试图克隆一个<div>,这样可以在它的<button>个子系统上保存事件侦听器,我认为DOM没有这样的方法.相反,您必须将相同的事件侦听器附加到新克隆的按钮.如下所示:

// Fetch the container and save that element as a constant for future use
const container = document.querySelector('#container')
// Create the addHandlers function for future use
function addHandlers (span) {
  // Find the first button child of the element provided as first parameter
  const button = span.querySelector('button')
  // Add a click event listener to that button
  button.addEventListener('click', () => {
    // On click, clone the parent element (the span, not the button)
    const clone = span.cloneNode(true)
    // Find the first input child of this new clone
    const input = clone.querySelector('input')
    // Set its value to the empty string (by default, the clone would conserve the value of the original)
    input.value = ''
    // Add handlers to the clone which were lost during cloning
    addHandlers(clone)
    // Add the clone into the DOM as a new, last child of the container
    container.appendChild(clone)
  }, false)
}
// Find all elements with class name 'duplicatable' and run the `addHandlers` function on each element
document.querySelectorAll('.duplicatable').forEach(addHandlers)
<div id="container">
  <h2>Characters</h2>
  <span class="duplicatable">
    <button>Add a new character</button>
    <input type='text' placeholder='name' />
  </span>
</div>

Javascript相关问答推荐

如何使用合并排序方法编写数组排序Polyfill

使用jsPDF添加Image JPEG将p5.js草图画布下载为PDF

ChartJS:分组堆叠条形图渲染错误

我的JS代码将按照哪个序列被解释

vue3类型脚本中可能未定义对象''

序列查找器功能应用默认值而不是读取响应

JavaScript文本区域阻止KeyDown/KeyUp事件本身上的Alt GR +键组合

可以的.是否可以在不预编译的情况下使用嵌套 Select 器?

vscode扩展-webView Panel按钮不起任何作用

传递一个大对象以在Express布局中呈现

react—router v6:路由没有路径

如何修复(或忽略)HTML模板中的TypeScript错误?'

引用在HTMLAttributes<;HTMLDivElement>;中不可用

使用GraphQL查询Uniswap的ETH价格

如何避免页面第一次加载时由于CSS样式通过JavaScript更改而出现闪烁

编辑文本无响应.onClick(扩展脚本)

使用带有HostBinding的Angular 信号来更新样式?

如何确保预订系统跨不同时区的日期时间处理一致?

通过跳过某些元素的对象进行映射

更改agGRID/Reaction中的单元格格式