从数组中随机生成图片和文本的脚本

时间:2012-04-05 14:30:41

标签: javascript html

我实际上是在设置它,以便当我点击我网站上的按钮时,页面会刷新一个来自数组的随机文本和一个来自数组的随机图像。我已将文本部分缩小,但需要知道如何处理图片部分。这是我现在用于文本生成器的脚本:

<script language="JavaScript">
var r_text = new Array ();
r_text[0] = "You'll want to track-change your life";
r_text[1] = "A misplaced comma can get you fired";
r_text[2] = "Work-life balance? Ha!";
r_text[3] = "Even the bankers laugh at you";
r_text[4] = "There's a gym so you can't leave the building";
r_text[5] = "Student debt can't be discharged";
r_text[6] = "Sallie Mae becomes one of your biggest contacts";
r_text[7] = "Good luck with partner track";
r_text[8] = "There are no lifestyle firms";
r_text[9] = "Your soul will die";
r_text[10] = "Socratic Method? More like Suck-ratic Method";
r_text[11] = "So much paper. You'll be destroying rainforests";
var i = Math.floor(12*Math.random())
document.write(r_text[i]);
</script>

关于如何在这里获取图像的任何想法?

5 个答案:

答案 0 :(得分:1)

var r_images = new Array (); 
r_images[0] = "a.jpg";
r_images[1] = "b.jpg;
.
.
.


var i = Math.floor(12*Math.random())
document.write(r_text[i]);
document.write("<img src='"+images_folder+r_images[i]+"' />");

答案 1 :(得分:0)

只是将img标签放在同一个数组中,因此它与文本混合:

....
r_text[12] = "<img src='1.gif' />";
r_text[13] = "<img src='2.gif' />";
...

等...

答案 2 :(得分:0)

你能为图像做同样的事情吗?

var imageNames = ['life', 'fired', 'balance', 'bakers', 'gym']
var i = Math.floor(5*Math.random())
document.write("<img src='http://path/to/your/image/" + imageNames[i] + ".jpg'>");

答案 3 :(得分:0)

为图片设置一个文件夹,然后给它们随机名称12,13,14 .......比文字一样

var i = Math.floor(24*Math.random())
document.write("<img src='folder-path/" + r_text[i] + ".jpg'>");

答案 4 :(得分:0)

我发现这是一篇旧帖子,但对于未来的读者,请考虑使用innerHTML代替document.write,我建议将var i = Math.floor(12*Math.random())更改为var i = Math.floor(r_text.length * Math.random());您添加了更多图片,而不必担心更改*Math.random())

旁边的数字

在此示例中,我使用了Array对象,以便您可以轻松地为每个图像定义alt文本(针对那些验证标记)。

对于任何关心工作范例的人:

http://jsfiddle.net/GmZPe/3/

<强> HTML

<div id="container"></div>

<强> JS

var r_img = [];
r_img[0] = {
    source:
        "image01.png",
    altText:
        "image 01"
};
r_img[1] = {
    source:
        "image02.png",
    altText:
        "image 02"
};
r_img[2] = {
    source:
        "image03.png",
    altText:
        "image 03"
};

var i = Math.floor(r_img.length * Math.random());
var s = "<img src='http://tinyurl.com/ky3nmqp/" + r_img[i].source + "' alt='" + r_img[i].altText + "'>";
document.getElementById('container').innerHTML = s;

或者如果您愿意,我们可以在事件发生时将其包装在函数调用中:

http://jsfiddle.net/GmZPe/4/

<强> HTML

<div id="container"></div>
<button onclick="myFunction()">Click me</button>

<强> JS

function myFunction() {
    var r_img = [];
    r_img[0] = {
        source:
            "image01.png",
        altText:
            "image 01"
    };
    r_img[1] = {
        source:
            "image02.png",
        altText:
            "image 02"
    };
    r_img[2] = {
        source:
            "image03.png",
        altText:
            "image 03"
    };

    var i = Math.floor(r_img.length * Math.random());
    var s = "<img src='http://tinyurl.com/ky3nmqp/" + r_img[i].source + "' alt='" + r_img[i].altText + "'>";
document.getElementById('container').innerHTML = s;
}