简体   繁体   English

在按钮上单击 100 次后显示图像

[英]Displaying an image after clicking 100 times on a button

I'm new here and I still have some difficulties in coding.我是新来的,在编码方面仍然有一些困难。 I'm trying to create an html page for some friends and I managed to create a click counter, an image which appear and disapear after some time etc我正在尝试为一些朋友创建一个 html 页面,我设法创建了一个点击计数器,一个在一段时间后出现和消失的图像等

However the only thing that I can't manage to do is how I can make an image appear after clicking on the button for 100 or 1000 times.然而,我唯一不能做的是如何在单击按钮 100 或 1000 次后显示图像。 I can make the image appear after clicking on the button one time, but I don't know how to make it appear only after some clicking.单击一次按钮后,我可以使图像出现,但我不知道如何使其仅在单击后才出现。

If someone can help me I'll be very glad!如果有人可以帮助我,我会很高兴!

 $button = document.querySelector('button') $span = document.querySelector('span') function increment() { $span.innerHTML++; } $button.addEventListener('click', increment); document.addEventListener("DOMContentLoaded", function() { setTimeout(function() { showImage(); setInterval(hideImage, 8000); }, 5000); }); function hideImage() { document.getElementById("imgHideShow").style.display = "none"; } function showImage() { document.getElementById("imgHideShow").style.display = "block"; }
 <img class="prayme" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Tram_icon_black_and_transparent_background.svg/1024px-Tram_icon_black_and_transparent_background.svg.png"> <p>You prayed <span id='count'>0</span> times</p> <div id="image"> <img src="https://pbs.twimg.com/profile_images/998926585691451392/WlkEVV7x_400x400.jpg"> </div> <div class="text-center"> <button><img class="imgbutton" src="https://www.nicepng.com/png/detail/980-9803933_emoji-emoji-pray-thankyou-thanks-praying-hands-emoji.png"> Afficher l'image </button> </div> <div> <img src="https://i.stack.imgur.com/1dpmw.gif" class="browse-tip" id="imgHideShow"> </div>

You just need to add an if statement, checking if the innerHTML is more or equal to 100, and then call showImage() .您只需要添加一个 if 语句,检查 innerHTML 是否大于或等于 100,然后调用showImage()

I removed code that wasn't relevant.我删除了不相关的代码。

I added declarations to the variables by adding let in front the name.我通过在名称前面添加let来向变量添加声明。

I removed the button, and put an event listener directly on the image instead.我删除了按钮,而是直接在图像上放置了一个事件侦听器。

I think the rest of the code is self-explanatory.我认为代码的 rest 是不言自明的。

 let spanElement = document.querySelector('span'); let imgButton = document.getElementById('imgbutton'); function increment() { spanElement.innerHTML++; if (spanElement.innerHTML >= 5) { showImage(); } } imgButton.addEventListener('click', increment); document.addEventListener("DOMContentLoaded", function() { setInterval(hideImage, 8000); }); function hideImage() { document.getElementById("imgHideShow").style.display = "none"; } function showImage() { document.getElementById("imgHideShow").style.display = "block"; }
 img { height: 3rem; } #imgHideShow.hidden { display: none; }
 <img class="prayme" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Tram_icon_black_and_transparent_background.svg/1024px-Tram_icon_black_and_transparent_background.svg.png"> <p>You prayed <span id='count'>0</span> times</p> <div class="text-center"> <img id="imgbutton" src="https://www.nicepng.com/png/detail/980-9803933_emoji-emoji-pray-thankyou-thanks-praying-hands-emoji.png"> Afficher l'image </div> <div> <img src="https://i.stack.imgur.com/1dpmw.gif" class="hidden browse-tip" id="imgHideShow"> </div>

To make the code even more readable, I would also add the following it it like this:为了使代码更具可读性,我还将像这样添加以下内容:

  1. Add a constant for how many times the user need to click.为用户需要点击多少次添加一个常量。
  2. Declare variables for all the elements that are affected.为所有受影响的元素声明变量。
  3. Use a class ( .hidden ) to hide the image, and add/remove that class, instead of adding a style.使用 class ( .hidden ) 隐藏图像,并添加/删除 class,而不是添加样式。 You should only add a style if you can't toggle classes.如果您无法切换类,则只应添加样式。

 const TARGET_TO_SHOW_IMAGE = 5; let spanElement = document.querySelector('span'); let imgButton = document.getElementById('imgbutton'); let imgHideShow = document.getElementById("imgHideShow"); let numberOfTimesClicked = 0; function increment() { numberOfTimesClicked++; if (numberOfTimesClicked >= TARGET_TO_SHOW_IMAGE) { showImage(); numberOfTimesClicked = 0; // resets } spanElement.innerHTML = numberOfTimesClicked; } imgButton.addEventListener('click', increment); document.addEventListener("DOMContentLoaded", function() { setInterval(hideImage, 8000); }); function hideImage() { imgHideShow.classList.add('hidden'); } function showImage() { imgHideShow.classList.remove('hidden'); }
 img { height: 3rem; } #imgHideShow.hidden { display: none; }
 <img class="prayme" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Tram_icon_black_and_transparent_background.svg/1024px-Tram_icon_black_and_transparent_background.svg.png"> <p>You prayed <span id='count'>0</span> times</p> <div class="text-center"> <img id="imgbutton" src="https://www.nicepng.com/png/detail/980-9803933_emoji-emoji-pray-thankyou-thanks-praying-hands-emoji.png"> Afficher l'image </div> <div> <img src="https://i.stack.imgur.com/1dpmw.gif" class="hidden browse-tip" id="imgHideShow"> </div>

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

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