简体   繁体   English

为什么在加载页面时不会首先显示第一张图片? 我的 javaScript 出了什么问题?

[英]Why won't the first image at first when loading the page? What is wrong with my javaScript?

I am working with wordpress theme twenty twenty-one for the first time and trying to create a slide show of images.. The problem that I am having is that when the page first appears no image is shown.我第一次使用 wordpress 主题二十二十一并尝试创建图像的幻灯片放映。我遇到的问题是当页面首次出现时没有显示图像。 When you click on the button then the first images shows up.当您单击按钮时,会显示第一张图像。 Also when you get to the last image I want the first image in the array to show up, but instead no image shows up and you would have to click on the "previous arrow button" that I have created to see the last image again.此外,当您到达最后一张图像时,我希望显示数组中的第一张图像,但没有图像显示,您必须单击我创建的“上一个箭头按钮”才能再次查看最后一张图像。 First time working with the wordpress theme so I am thinking that something is overriding my script or that my script is wrong.第一次使用 wordpress 主题,所以我认为某些东西正在覆盖我的脚本或者我的脚本是错误的。

<div>

   <img class="exe1-img" 
   src="https://www.ttttea.co.uk/wp-content/uploads/2021/05/fullsizeoutput_8ca.jpeg" alt="">

   <img class="exe1-img" src="https://www.ttttea.co.uk/wp-content/uploads/2021/05/fullsizeoutput_945.jpeg" alt="">


  <button class="previous-img" onclick="plusDivs(-1)">❮ </button
  button class="next-img" onclick="plusDivs(1)"> ❯</button>

</div>

<script>
    let slideIndex = 1;

        showDivs(slideIndex);

        function plusDivs(n) { 
           
            showDivs(slideIndex += n);
}

       function showDivs(n) {
            let i;
            let x = document.getElementsByClassName("exe1-img");

            if (n > x.length) {
                slideIndex = 1
            }        
            if (n < 1) {
               slideIndex = x.length
            }        
            for (i = 0; i < x.length; i++) {
               x[i].style.display = "none";   
            }
            x[slideIndex-1].style.display = "block";  
            }
</script>

Had also had an issue with getting the automatic p tags out of the code in wordpress so had to write the script in single line.从 wordpress 的代码中获取自动 p 标签也存在问题,因此必须在单行中编写脚本。 Apologies for the readability.为可读性道歉。

After replacing the images (yours were not resolving) and fixing the broken tags (which I think was an artifact of the way it was pasted into your question without using the {} button), it seems to work.在替换图像(您的图像没有解决)并修复损坏的标签(我认为这是在不使用{}按钮的情况下将其粘贴到您的问题中的方式的产物)之后,它似乎有效。 Look at the snippet below and see if it's not doing what you want it to.查看下面的代码段,看看它是否没有按照您的意愿执行。

 let slideIndex = 1; showDivs(slideIndex); function plusDivs(n) { showDivs(slideIndex += n); } function showDivs(n) { let i; let x = document.getElementsByClassName("exe1-img"); if (n > x.length) { slideIndex = 1 } if (n < 1) { slideIndex = x.length } for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } x[slideIndex - 1].style.display = "block"; }
 <div> <img class="exe1-img" src="https://images.unsplash.com/photo-1621343498292-eb469c743b25" height='100' alt=""> <img class="exe1-img" height='100' src="https://images.unsplash.com/photo-1621347797284-abb4b9d4ad40" alt=""> <button class="previous-img" onclick="plusDivs(-1)">❮ </button> <button class="next-img" onclick="plusDivs(1)"> ❯</button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM