我想讓eventlistener爲循環內創建的類工作。我無法得到它的工作,有什麼建議嗎?for循環後的addEventListener類;香草JS
window.onload = function(){
var div = document.createElement('div'),
container = document.getElementById('container'),
classname = document.getElementsByClassName("hello");
div.className = 'hello';
var tasks = [
'One',
'Two',
'Three',
'Four'
];
for (var i = 0; i < tasks.length; ++i) {
container.innerHTML += '<div class="hello"><p>' + tasks[i] + '</p></div>';
}
classname.addEventListener('click', function(){
this.className = 'done';
});
console.log(classname);
};
謝謝主席先生,這工作就像一個魅力!將不得不閱讀NodeList,所以我可以理解:) – NinjaGrisen