【问题标题】:Randomly choose img between foldersl in html在html中的foldersl之间随机选择img
【发布时间】:2017-06-02 13:06:18
【问题描述】:

我有大量代码要编程,但我被卡住了。

我正在做一个包含 4 个文档的游戏:html、css、javascript 和 jquery。该游戏是关于尝试用不同的图像猜测世界上 20 个不同的国家。我被困在我不知道我必须做什么的点上,所以游戏的每一轮它都会在图片之间自动选择。另外,它只需要显示第一张图片,如果玩家点击按钮获得提示,则其他图片开始显示。

我所有的图片都是这样组织的: 图片/ 图片/国家/地区/ img/国家/地区名称01.jpg img/国家/地区名称02.jpg img/国家名称/国家名称03.jpg img/countryname/countryname04.jpg

在我的 html 文档中,我创建了以下 div:

<pre>
<div id="country">
    <div id="hint0">
        <!--choose randomly "img/countryname/countryname01.jpg"-->
    </div>
    <div id="hint1" class="hint" style="display: none">
        <!--choose randomly "img/countryname/countryname02.jpg"-->
    </div>
    <div id="pista2" class="pista" style="display: none">
        <!--choose randomly "img/countryname/countryname03.jpg"-->
    </div>
    <div id="pista3" class="pista" style="display: none">
        <!--choose randomly "img/countryname/countryname03.jpg"-->
    </div>
</div>
</pre>

【问题讨论】:

标签: javascript jquery html css image


【解决方案1】:

首先,您可以使用此函数获取一个随机数(根据您的上下文获取 1,4 之间的数字)

function getRandomArbitrary(min, max) {
    return "0"+parseInt(Math.random() * (max - min) + min);
}

然后你可以用随机的src 附加到div 一个img,例如:

var src = "img/countryname/countryname"+getRandomArbitrary(1,4)+".jpg";
$("#pista3").append('<img id="theImg" src="'+src+'" />');

显然您需要根据您的上下文调整这些代码,但就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多