JavaScriptを勉強している者です。
下記コードの一番下の箇所ですが、参照がulとなっているのに、どうして子要素のliをクリックしたときに hide関数が発火するのでしょうか?
ul.addEventListener('click', hide, false);
https://developer.mozilla.org/en-US/docs/Web/API/Event/target
// Make a list var ul = document.createElement('ul'); document.body.appendChild(ul); var li1 = document.createElement('li'); var li2 = document.createElement('li'); ul.appendChild(li1); ul.appendChild(li2); function hide(e){ // e.target refers to the clicked <li> element // This is different than e.currentTarget which would refer to the parent <ul> in this context e.target.style.visibility = 'hidden'; } // Attach the listener to the list // It will fire when each <li> is clicked ul.addEventListener('click', hide, false);
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/30 06:40