Html:
<ul> <li>Example li</li> <li>Example li</li> <li>Example li</li> <li>Example li</li> <li>Example li</li> <li>Example li</li> <li>Example li</li> </ul>
Js:
var ul = document.getElementsByTagName('ul'); ul.onclick = function(event) { alert(); }
alert
not displayed when clicking on the elements <li>
But if you assign the ul
id
to the element and search through document.getElementById
, then the code will work. Why is that?
var ul = document.getElementsByTagName('ul')[0]
- Alexander Igorevichwindow.onload = function () {ваш код}
- user33274