【发布时间】:2020-05-13 15:56:00
【问题描述】:
我构建了一个小应用程序来在 iframe 中的随机图像之间切换,我希望在 10 或 20 张图像之后,用户将获得我希望他获得的图像或来源,而不是随机图像,然后返回环形。 我对计数有问题,如果功能,将不胜感激。谢谢
<body>
<iframe id="img_main" src="https://www.example.com/img_4.jpg" width="600" height="800" frameborder="1" scrolling="no"></iframe>
<br>
<button id="H" type="button" onclick=(newImg(),clickCounter(),changeImg())>images</button>
<div id="result"></div>
<script>
function newImg(){
var myArray = [
"img_1.jpg",
"img_2.jpg",
"img_3.jpg",
"img_4.jpg"
];
var imgNew = "https://example.com/"
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
document.getElementById("img_main").src = "https://example.com/" + randomItem ;
}
function clickCounter() {
if (typeof(Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount)+1;
} else {
localStorage.clickcount = 1;
}
function changeImg(){
if (localStorage.clickcount = 10,20) {
document.getElementById("ins_main").src = "https://example.com/pre_defined_img.jpg";
}
}
</script>
</body>
【问题讨论】:
-
img.com 在这种情况下只是一个占位符。如果您能帮助编写脚本,我将不胜感激。
-
你说得对,应该是一样的。两个地方都应该是 img_main 。这是一个错字。
-
已修复,感谢您的耐心。
-
我猜你没有在下面看到我的答案...:/
-
对不起,我刚看到,试图理解它:)谢谢你的帮助。
标签: javascript arrays events