1

我实际上是在尝试对其进行设置,以便当我单击网站上的按钮时,页面会使用数组中的随机文本和数组中的随机图像刷新。我已经把文字部分写下来了,但需要知道如何做图片部分。这是我现在用于文本生成器的脚本:

<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>

关于我可以做些什么来在这里获取图像的任何想法?

4

5 回答 5

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]+"' />");
于 2012-04-05T14:36:27.533 回答
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;
}
于 2013-10-10T18:06:23.147 回答
0

只需将 img 标签放在同一个数组中,这样它就可以与文本混合:

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

ETC....

于 2012-04-05T14:34:10.230 回答
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'>");
于 2012-04-05T14:37:28.330 回答
0

为图像设置一个文件夹,并给它们随机命名 12,13,14 .......比相同的文本

var i = Math.floor(24*Math.random())
document.write("<img src='folder-path/" + r_text[i] + ".jpg'>");
于 2012-04-05T14:44:37.940 回答