【问题标题】:Javascript: displaying random images based on user inputJavascript:根据用户输入显示随机图像
【发布时间】:2014-05-15 18:38:25
【问题描述】:

因此,我将创建一个锻炼网站作为一种爱好,并且在锻炼创建者方面需要一些帮助。所以我有一个页面,我想根据一个人的限制随机创建一个锻炼。这是该部分的网页:random workout creator。基本上,当一个人选择了锻炼限制后,他们会点击“创建锻炼”按钮,然后页面会显示10张不同的锻炼图片供用户完成。所以这是我的问题:当我输入“无限制”并创建锻炼时,只显示一个图像,而不是 10 个。当我输入“怀孕”或“低影响”时,单击“创建锻炼”按钮,没有任何反应.任何人都可以查看我的 javascript 并查看我做错了什么吗?这是javascript的代码:

function createOffice(){
var randomimages=new Array();

randomimages[0]="images/office/arm circles.gif"
randomimages[1]="images/office/calf raises.gif"
randomimages[2]="images/office/chair step ups.gif"
randomimages[3]="images/office/curtsy lunge.gif"
randomimages[4]="images/office/desk pushups.gif"
randomimages[5]="images/office/lunge2.gif"
randomimages[6]="images/office/Lunges.gif"
randomimages[7]="images/office/side lunge.gif"
randomimages[8]="images/office/Squat.gif"
randomimages[9]="images/office/squat punches.gif"
randomimages[10]="images/office/Step up with knee up.gif"
randomimages[11]="images/office/tricep dips.gif"
randomimages[12]="images/office/Butt Kickers.gif"
randomimages[13]="images/office/high knees.gif"
randomimages[14]="images/office/jogging in place.gif"
randomimages[15]="images/office/jumping butt kicks.gif"
randomimages[16]="images/office/jumping jacks.gif"
randomimages[17]="images/office/jumping rope.gif"
randomimages[18]="images/office/Star Jumps.gif"
randomimages[19]="images/office/wall jump touch.gif"

var preload=new Array()

for (n=0;n<randomimages.length;n++){
    preload[n]=new Image()
    preload[n].src=randomimages[n]
}

if(document.getElementById("impact").checked == false && document.getElementById("pregnant").checked == false && document.getElementById("none").checked == false){
    document.getElementById("error").innerHTML = "*All fields are required";
}
else{
    if(document.getElementById("pregnant").checked == true || document.getElementById("impact").checked == true){
        document.getElementById("img1").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img2").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img3").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img4").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img5").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img6").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img7").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img8").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img9").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')
        document.getElementById("img10").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">')       
    }
    else{
        document.getElementById("img1").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img2").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img3").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img4").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img5").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img6").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img7").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img8").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img9").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')
        document.getElementById("img10").innerHTML = document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*20)]+'">')       
    }
}
}

【问题讨论】:

  • 像@rla4 说的那样去掉你的document.writes,也像@eventHandler 说的那样使用分号,但你也应该记住,这些可能会显示相同的图像。

标签: javascript html css arrays image


【解决方案1】:

首先,您必须创建要修改的 img divs:

<div id="img1"></div>
<div id="img2"></div>
<div id="img3"></div>
<div id="img4"></div>
<div id="img5"></div>
<div id="img6"></div>
<div id="img7"></div>
<div id="img8"></div>
<div id="img9"></div>
<div id="img10"></div>

然后,替换每一行

document.getElementById("img1").innerHTML = document.write(<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">)

与

document.getElementById("img1").innerHTML = '<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*12)]+'">' 

请记住,document.write 不会返回任何内容,因此您不能将其设置为您的 innerHTML。

【讨论】:

  • 所以我这样做了,图像仍然不显示。现在,当我单击“创建锻炼”时,无论我检查了哪些限制,什么都没有发生
  • document.getElementById("img1") 返回 null。在尝试修改它们之前,您必须创建 img 标签。另外,我在上次编辑时错误地删除了 img 标签本身的创建。
【解决方案2】:

您的代码有很多问题。就像eventHandler 说的,你应该有分号,就像rla4 说你需要删除document.write 函数。

我建议完全不同的实现方式。

您的实现将重用图像,我认为您不希望这样做。

这是一个不会重复使用图像的函数:

function randoms(many) {
    var arr = randomImages,
        i;
    while (many < (arr.length)) {
        arr.splice(Math.floor(Math.random() * (arr.length - 1)), 1);
    }
    return arr;
}

然后你可以将它添加到你的页面:

randoms(10).forEach(function (img) {
    var elem = document.createElement('img');
    elem.src = img;
    document.getElementById('images').appendChild(elem);
});

它正在运行:JSFiddle for Random Images

【讨论】:

  • 我实际上确实希望它能够多次显示同一图像。我改变了它,添加了 ;并摆脱了document.write,但它仍然无法正常工作
猜你喜欢
  • 2013-11-10
  • 2020-04-17
  • 2015-02-01
  • 2014-10-16
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
相关资源
最近更新 更多