Escribí un programa html css muy simple y agregué un javascript en él, aquí está mi programa y mi javascript en él
(mi programa: https://github.com/phianh2904/phan-8-web-the-band-tablet-responsive )
mi javacript en el archivo index.html
<script> var header = document.getElementById('header'); var mobileMenu = document.getElementById('mobile-menu'); mobileMenu.onclick = function() { console.log(header.clientHeight); } const buyBtns = document.querySelectorAll('.js-buy-ticket') const modal = document.querySelector('.js-modal') const modalclose = document.querySelector('.js-modal-close') const modalcontainer = document.querySelector('.js-modal-container') // ham hien thi modal mua ve tuc la them class open vao modal function showBuyTickets() { modal.classList.add('open') } // ham an modal mua ve them class open vao modal function hideBuyTickets() { modal.classList.remove('open') } // lap qua tung the button va nghe hanh vi click for (const buyBtn of buyBtns) { buyBtn.addEventListener('click', showBuyTickets) } // nghe hanh vi click vao button close modalclose.addEventListener('click', hideBuyTickets) //clcik vao khoang khong de dong cac the con lai modal.addEventListener('click', hideBuyTickets) //click vao ben trong thi khong bi lam sao ca modalcontainer.addEventListener('click', function(event) { event.stopPropagation() }) </script>Como puede ver en mi fragmento de código javascript, es muy simple. Escribí el código
var header = document.getElementById('header'); var mobileMenu = document.getElementById('mobile-menu'); mobileMenu.onclick = function() { console.log(header.clientHeight); }para mostrar la altura del cliente.
Pero aquí está el error que tuve:
TypeError no capturado: no se pueden establecer propiedades de nulo (configurando 'onclick') en index.html: 231: 28
Como dijeron en este enlace TypeError no detectado: no se puede establecer la propiedad 'onclick' de nulo
Puse el script al final del elemento del cuerpo y envolví el código, pero no funcionó. Entonces, ¿podría darme alguna respuesta para este problema? Muchas gracias por tu tiempo.