简体   繁体   English

在JavaScript中克隆后,classList.toggle()无法正常工作

[英]classList.toggle() not working after cloning in JavaScript

I have the following div: 我有以下div:

<div id="template">

    <label><h1>Enter your title</h1></label>
    <input type="text" name="title[]" placeholder="Enter your title">
    <label><p>Enter your text</p></label>
    <textarea type="text" name="textbox1[]" placeholder="Enter your text"></textarea>

</div>

I clone this div and append it using JavaScript. 我克隆了这个div并使用JavaScript附加了它。 On click on a button, the div is supposed to open by toggling the .open class. 在单击按钮时,div应该通过切换.open类来打开。

Relevant JavaScript: 相关JavaScript:

function addClonedDiv() {

    var div = document.getElementById('template');
    window.clonedDiv = div.cloneNode(true);
    document.getElementById('otherDiv').appendChild(clonedDiv);
    clonedDiv.firstChild.className = "newDiv";
}

function addIframe() {

    clonedDiv.firstChild.classList.toggle("open");

}

(There's two buttons, one clones the div and the other one shows it, it's hidden by default, otherDiv is the div I want to append the new div to. I add the class="newDiv" to it because I will need it later for other purposes which are not relevant to this question, however, I wanted to include it as I don't know if that may be the problem). (有两个按钮,一个克隆div,另一个显示它,默认情况下它是隐藏的,otherDiv是我要将新div附加到的div。我向其添加class =“ newDiv”,因为以后会用到它出于与该问题无关的其他目的,但是,我想将其包括在内,因为我不知道这可能是问题所在)。 The .open class in CSS: CSS中的.open类:

.open {
    visibility: visible;
    width: 100%;
}

However, when I click on the button that is supposed to show the div, I get this error message in the console: Cannot read property 'toggle' of undefined. 但是,当我单击应该显示div的按钮时,我在控制台中收到以下错误消息:无法读取未定义的属性“ toggle”。 I'm not sure why the new div would be undefined and how to fix that. 我不确定为什么新的div是不确定的,以及如何解决这个问题。

I read through other questions on here that said that I have to deep clone the div, which I tried applying (as you can see), but I still received the same error message. 我在这里阅读了其他问题,说我必须深度克隆div,我尝试应用了它(如您所见),但是我仍然收到相同的错误消息。 I know cloning doesn't clone event-handlers, but I'm pretty sure, that that is not the problem in my case. 我知道克隆不会克隆事件处理程序,但是我很确定,这不是我的问题。

I did read through plenty of other questions and did my research, however, I could not fix the problem. 我确实阅读了许多其他问题并进行了研究,但是,我无法解决问题。 I definitely want to solve this problem in JavaScript and not jQuery. 我绝对想用JavaScript而不是jQuery解决此问题。

A few problems here (and even more in your fiddle, but since you seem to have gotten until the error message you quoted, I'll fix the ones of the fiddle for free). 这里有一些问题(甚至在您的小提琴中还有更多问题,但是由于您似乎在引述错误消息之前就已经明白了,所以我将免费修复小提琴的问题)。

Error repro: 错误再现:

 function addClonedDiv() { var div = document.getElementById('template'); window.clonedDiv = div.cloneNode(true); document.getElementById('otherDiv').appendChild(clonedDiv); clonedDiv.firstChild.className = "newDiv"; }; // open is reserved function open_() { console.log(clonedDiv); clonedDiv.firstChild.classList.toggle("open"); }; 
 #template { visibility: hidden; } .newDiv.open { visibility: visible; } 
 <div id="otherDiv"> <button onclick="addClonedDiv()"> Add Item </button> <button onclick="open_()"> Open </button> </div> <div id="template"> <label><h1>Enter your title</h1></label> <input type="text" name="title[]" placeholder="Enter your title"> <label><p>Enter your text</p></label> <textarea type="text" name="textbox1[]" placeholder="Enter your text"></textarea> </div> 


So let's first fix the error message: 因此,让我们首先修复错误消息:

clonedDiv.firstChild is a Node, and even a textNode, representing a new line character. clonedDiv.firstChild是一个Node,甚至是textNode,代表换行符。 So you can set a property to it, but it doesn't inherit from Element nor from HTMLElement prototypes and thus doesn't have a classList property. 因此,您可以为其设置属性,但它既不继承自Element也不继承自HTMLElement原型,因此不具有classList属性。

What you want is clonedDiv.firstElementChild (in addCloneDiv too). 您想要的是clonedDiv.firstElementChild (也在addCloneDiv )。

Then, Node.cloneNode will copy all the attributes of an Element, when called on an Element. 然后, Node.cloneNode将在元素上调用时复制元素的所有属性。 So your clonedDiv will also have the id="template" attribute. 因此,您的clonedDiv也将具有id="template"属性。 This means that your .newDiv.open rule will have less importance than the #template one. 这意味着您的.newDiv.open规则的重要性不如#template规则重要。

 function addClonedDiv() { var div = document.getElementById('template'); window.clonedDiv = div.cloneNode(true); document.getElementById('otherDiv').appendChild(clonedDiv); clonedDiv.firstElementChild.className = "newDiv"; // grab the first Element clonedDiv.removeAttribute('id'); // ids must be unique per document }; function open_() { clonedDiv.firstElementChild.classList.toggle("open"); }; 
 #template, .newDiv { visibility: hidden; } .newDiv.open { visibility: visible; } 
 <div id="otherDiv"> <button onclick="addClonedDiv()"> Add Item </button> <button onclick="open_()"> Open </button> </div> <div id="template"> <label><h1>Enter your title</h1></label> <input type="text" name="title[]" placeholder="Enter your title"> <label><p>Enter your text</p></label> <textarea type="text" name="textbox1[]" placeholder="Enter your text"></textarea> </div> 

Now I'm not sure this is really what you wanted to occur, but I'll let you find yourself. 现在,我不确定这是否确实是您想要发生的情况,但我会让您发现自己。

try this: 尝试这个:

 var clonedDiv; function clone(){ clonedDiv = document.getElementById('template').cloneNode(true); document.getElementById('otherDiv').appendChild(clonedDiv); clonedDiv.className = "newDiv"; } function show(){ (!clonedDiv.classList.contains("open")) ? clonedDiv.classList.add("open"):clonedDiv.classList.remove("open"); } 
 .newDiv{ visibility: hidden; } .open { visibility: visible; width: 100%; } 
 <div id="template"> <label><h1>Enter your title</h1></label> <input type="text" name="title[]" placeholder="Enter your title"> <label><p>Enter your text</p></label> <textarea type="text" name="textbox1[]" placeholder="Enter your text"></textarea> <button onclick="clone()">clone</button> <button onclick="show()">show</button> </div> <div id="otherDiv"> </div> 

instead of toggle 而不是切换

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

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