A question. Why can't I write style.block to slides?

var slideIndex = 1; showSlides(slideIndex); function plusSlides(n){ showSlides(slideIndex += n); } function currentSlide(n){ showSlides(slideIndex = n); } function showSlides(n){ var i; var slides = document.getElementsByClassName("mySlides"); var dot = document.getElementsByClassName("dot"); if(n > slides.length){ slideIndex = 1; } if(n < 1){ slideIndex = slides.length; } for(i=0; i <slides.length; i++){ slides[i].style.display = "none"; } for(i = 0; i < dot.length; i++){ dot[i].className = dot[i].className.replace("active", ""); } slides[slideIndex-1].style.display = 'block'; dot[slideIndex-1].className += "active"; } 

Closed due to the fact that the essence of the question is not clear to the participants: Igor , Vadim Ovchinnikov , Denis Bubnov , user194374, fori1ton Jan 20, 17 at 18:15 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • The question is incomprehensible, explain everything in more detail and in general what this piece of code does - andreymal
  • one
    what mean can not? - titov_andrei
  • In detail. When I write slides [slideIndex-1] .style.display = 'block'; browser error. - Vitalii
  • This piece of code for the slider - Vitalii
  • It is easiest to handle adding a class to a given element than doing it through js. Those. what was done with dot , and with slides , hang a new class on the object, something like slideShow/slideHide , which will be described in css with the display:block; / display:none rule display:block; / display:none display:block; / display:none . Well, the reason why it does not work. it may be that 1) the slides object can be empty (length = 0) 2) slideIndex can go beyond the array. - alexoander

1 answer 1

If I understand correctly, then you mean to set the style for a particular block. The fact is that in slides data is recorded because it refers immediately to all elements with the class mySlides (getElement s ByClassName). I think the following solution will suit you:

 ... slides[slideIndex-1].style.setAttribute("style", "display:block;"); dot[slideIndex-1]... 
  • doesn't work anyway - Vitalii
  • Attach the html code that relates directly to this script - Vedenin
  • Thank you, I have already solved the problem. - Vitalii