Sie haben eine Reihe von Problemen. so werden wir die Grundlagen der Suche beginnen aus dem JavaScript Sie auf dem Laufenden:
Array.from(document.getElementsByClassName('thisclass')).forEach(
currentNode => currentNode.addEventListener('click', function(){
// do whatever
});
Oder etwas kurz und bündig:
document.getElementByClassName("thisclass")
// ^- typo, the function is: getElementsByClassName
// (note the additional 's')
.addEventListener("click",function(){
// a NodeList - returned by the getElementsByCLassName()
// function has no addEventListener() method, so instead
// you have to iterate over all elements in the NodeList
// and apply the event-listener to each element individually.
//do something with the specific clicked div
});
So die oben wäre richtig geschrieben werden
document.querySelectorAll('.thisclass').forEach(
currentNode => currentNode.addEventListener('click', function(){
// do whatever
});
Welche nutzt die NodeList, die von document.querySelectorAll()
mit einer forEach()
Methode zurückgegeben wird, während die zurückgegebene Live NodeList zurückgegeben von document.getElementsByClassName()
nicht tut t.
Der andere Teil des Problems ist, dass Sie die Ereignis-Listener zu den Elementen hinzufügen möchten, die noch nicht im Dokument vorhanden sind.
Da JavaScript Ereignis-Listener nicht direkt an Elemente binden kann, die dem Dokument hinzugefügt werden, nachdem die Ereignis-Listener gebunden sind, müssen wir stattdessen den Ereignis-Listener zu einem vorhandenen Vorgängerelement auf der Seite hinzufügen zu den Elementen selbst.
// caching references to various elements for later use:
let button = document.querySelector('button'),
ancestor = document.body;
// named function to add new elements to the page;
// the first argument is the 'event' Object,
// passed automagically from the addEventListener()
// method:
function addNewElement(event) {
// because we're using the ancestor element of
// the <button> to listen for the same 'click'
// event, here we use event.stopPropagation()
// to avoid that click bubbling up through the
// document:
event.stopPropagation();
// we find the first element matching the supplied
// selector (or if no such element exists this
// will return null; so in production it's sensible
// to check the element exists):
let newElement = document.querySelector('.thisclass')
// and then we clone that Node:
.cloneNode(true);
// this is passed automagically from the
// addEventListener() method, and refers to
// the element on which the event-listener
// was fired; here we find the <button>
// element's parentNode and use the
// parentNode.appendChild() method to add
// the newly-created element to the document:
this.parentNode.appendChild(newElement);
// here we explicitly add a class to the
// dynamically added content, just to be
// explicit that it's a newly-added element:
newElement.classList.add('dynamicallyAdded');
}
// a simple named function to demonstrate
// simple functionality:
function handleThisclassClicks(event) {
// the event.target is the Node upon which
// the listened-for event was originally
// fired:
let target = event.target;
// here we simply toggle the 'active'
// class on the clicked element:
target.classList.toggle('active');
}
// binding the named ('addNewElement()') function
// as the event-handler for the click event on
// the button Node (note the deliberate lack of
// parentheses):
button.addEventListener('click', addNewElement);
// binding the named function as the event-handler for
// the click event fired on the ancestor Node (which is,
// again, why we used event.stopPropagation() in the
// button Node's click-handler):
ancestor.addEventListener('click', handleThisclassClicks);
let button = document.querySelector('button'),
ancestor = document.body;
function addNewElement(event) {
event.stopPropagation();
let newElement = document.querySelector('.thisclass')
.cloneNode(true);
this.parentNode.appendChild(newElement);
newElement.classList.add('dynamicallyAdded');
}
function handleThisclassClicks(event) {
let target = event.target;
target.classList.toggle('active');
}
button.addEventListener('click', addNewElement);
ancestor.addEventListener('click', handleThisclassClicks);
div {
color: red;
margin-left: 0;
transition: color 0.5s linear, margin-left 1s linear;
}
div.active {
color: limegreen;
margin-left: 1em;
}
.dynamicallyAdded::after {
content: '*';
}
<button>Add another '.thisclass' element</button>
<div class="thisclass">
HTML
</div>
<div class="thisclass">
HTML
</div>
<div class="thisclass">
HTML
</div>
Referenzen:
Bibliographie:
Ich verstehe die Frage nicht, es sieht aus wie Sie wissen und tat, was Sie –
tun wollen Nun, ich einen ähnlichen Code in meine Seite zu schreiben haben es versucht, aber es funktioniert nicht ... –
@ LukasMittunAlexanderGuldstv den Code zeigen Sie :) – ThisGuyHasTwoThumbs