¡Toda salud y felicidad! Un script escrito que debe iterar a través de todas las imágenes, si las imágenes no se cargaron, entonces el script debe ocultar el bloque principal en la clase .Parent_block , es decir, tengo 3 bloques, hay una imagen en el bloque central, pero no en los otros dos, lo que significa que deberían tener propiedad display:none
Pero hice algo mal. ¿Dónde cometí un error?
let parent_block = document.querySelectorAll('.Parent_block'); let image_b = document.querySelectorAll('.Child_Img'); image_b.forEach(function(){ if(image_b.onerror){ for(let parent_block of hide){ hide.style.display = 'none'; } } }) .Parent_block{ width:30%; margin:0; padding:0; margin-left:10px; margin-top:10px; border:1px solid black; display:inline-block; height:250px; } img{ height:inherit; width:100%; <figure class="Parent_block"> <!-- ***** this block should disappear ***** =( --> <img class="Child_Img" src=""> <figcaption class ="Img_text">1</figcaption> </figure> <figure class="Parent_block"> <img class="Child_Img" src="https://i.ibb.co/kyXhZmB/photo-2021-11-18-14-40-18.jpg"> <figcaption class ="Img_text">2</figcaption> </figure> <figure class="Parent_block"> <!-- ***** and this one too ***** =( --> <img class="Child_Img" src=""> <figcaption class ="Img_text">3</figcaption> </figure> let parent_block = document.querySelectorAll('.Parent_block'); let image_b = document.querySelectorAll('.Child_Img'); image_b.forEach(function(image){ // check if any error (if image doesn't load in our case) image.addEventListener("error",()=>{ // find closest class name and hide it image.closest(".Parent_block").style.display = "none"; }) }) .Parent_block{ width:30%; margin:0; padding:0; margin-left:10px; margin-top:10px; border:1px solid black; display:inline-block; height:250px; } img{ height:inherit; width:100%; <figure class="Parent_block"> <!-- ***** this block should disappear ***** =( --> <img class="Child_Img" src=""> <figcaption class ="Img_text">1</figcaption> </figure> <figure class="Parent_block"> <img class="Child_Img" src="https://i.ibb.co/kyXhZmB/photo-2021-11-18-14-40-18.jpg"> <figcaption class ="Img_text">2</figcaption> </figure> <figure class="Parent_block"> <!-- ***** and this one too ***** =( --> <img class="Child_Img" src=""> <figcaption class ="Img_text">3</figcaption> </figure>Hay algunos problemas con el guión.
image_b.forEach y dentro de eso está intentando acceder a image_b.onerror . Esto no funcionará ya que image_b es una matriz.onerror es un evento y no una propiedad. Por lo tanto, no se puede acceder de esta manera.for(let parent_block of hide) es incorrecta.Código fijo
Lógica
Child_Img .onerror en cada elemento de la lista que seleccionamos con el nombre de clase Child_Img .Parent_block y ocúltelo.violín de trabajo
let image_b = document.querySelectorAll('.Child_Img'); image_b.forEach(function (img) { img.onerror = function(image) { img.closest(".Parent_block").style.display = 'none'; } }) .Parent_block { width: 30%; margin: 0; padding: 0; margin-left: 10px; margin-top: 10px; border: 1px solid black; display: inline-block; height: 250px; } img { height: inherit; width: 100%; } <figure class="Parent_block"> <!-- ***** this block should disappear ***** =( --> <img class="Child_Img" src=""> <figcaption class="Img_text">1</figcaption> </figure> <figure class="Parent_block"> <img class="Child_Img" src="https://i.ibb.co/kyXhZmB/photo-2021-11-18-14-40-18.jpg"> <figcaption class="Img_text">2</figcaption> </figure> <figure class="Parent_block"> <!-- ***** and this one too ***** =( --> <img class="Child_Img" src=""> <figcaption class="Img_text">3</figcaption> </figure>Hay una serie de problemas con su script.
Algunos ejemplos:
onerror no debe verificarse con if , sino con event listener.forEach necesita un argumento, no ha proporcionado ninguno..onerror al argumento de foreach , y no directamente a image_bhide en su secuencia de comandos. Supongo que tenías la intención for (let hide of parent_block)hide.style.display = 'none' para todo parent_block, y no solo para los que incluyen las imágenes con errores.Lo siguiente debería funcionar:
let parent_block = document.querySelectorAll('.Parent_block'); let image_b = document.querySelectorAll('.Child_Img'); image_b.forEach(function (cur_image) { cur_image.onerror = function () { for(let block of parent_block){ if(block.contains(cur_image) ){ block.style.display = 'none'; } } } }) Pero el ciclo sobre parent_block no es necesario, solo puede encontrar el ancestro relevante de cur_image :
// let parent_block = document.querySelectorAll('.Parent_block'); let image_b = document.querySelectorAll('.Child_Img'); image_b.forEach(function (cur_image) { cur_image.onerror = function () { let hide = cur_image.closest('.Parent_block') hide.style.display = 'none'; } }) Tenga en cuenta que ni siquiera es necesario ejecutar document.querySelectorAll('.Parent_block') nunca más.