This is a jshint warning question.How can I solve this problem?
var comment_btn=document.querySelector('.comment_button'); var comment_ul=document.querySelector('.comment_ul'); var comment_text=document.querySelector('#comment'); comment_btn.onclick = function(){ var comment_li = document.createElement('li'); comment_li.className = 'comment_li'; if(comment_text.value != '') { comment_li.innerHTML = comment_text.value + "<a class='comment_a' href='javascript:;'>Delete</a>"; comment_ul.insertBefore(comment_li,comment_ul.children[0]); var del = document.querySelectorAll('.comment_a'); for (var i = 0; i < del.length; i++) { del[i].onclick = function() { comment_ul.removeChild(this.parentNode); }; } } else { alert('Please input!'); } }; Warning:
Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. (comment_ul) (W083)jshint(W083)
I really can't think of a solution,please help me.
var del = document.querySelectorAll('.comment_a');is going to select every element on the page. So if you clickcomment_btnmore than once you will be adding a lot of events. You should only be selecting theain the li you create. You should not have to loop at all.