简体   繁体   English

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

[英]Script to randomly generate pictures and text from an array

I'm essentially trying to set it up so that when I click a button on my site, the page refreshes with either a random piece of text from an array and a random image from an array.我实际上是在尝试设置它,以便当我单击网站上的按钮时,页面会使用数组中的随机文本片段和数组中的随机图像进行刷新。 I've got the text part down, but need to know how to do the picture part.我已经把文字部分弄下来了,但需要知道如何制作图片部分。 This is the script I'm using right now for the text generator:这是我现在用于文本生成器的脚本:

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

Any ideas on what I can do to get images in here?关于如何在此处获取图像的任何想法?

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]+"' />");

just put img tags in the same array, so it mixes with text:只需将 img 标签放在同一个数组中,以便它与文本混合:

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

etc.... ETC....

Can you do the same thing for an image?你能为图像做同样的事情吗?

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'>");

set a folder for images and give them random name onward 12,13,14.......than same like text为图像设置一个文件夹并给它们随机命名 12,13,14 ......比文本相同

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

I realize this is an old post, but for future readers, consider using innerHTML in lieu of document.write and also I recommend changing var i = Math.floor(12*Math.random()) to var i = Math.floor(r_text.length * Math.random());我意识到这是一篇旧文章,但对于未来的读者,请考虑使用innerHTML代替document.write并且我还建议将var i = Math.floor(12*Math.random())更改为var i = Math.floor(r_text.length * Math.random()); that way as you add more images you don't have to worry about changing the number next to *Math.random())这样,当您添加更多图像时,您不必担心更改*Math.random())旁边的数字

In this example I've used Array objects so that you can easily define the alt text (for those validation sticklers) for each image.在此示例中,我使用了 Array 对象,以便您可以轻松地为每个图像定义替代文本(对于那些验证贴纸)。

For anyone that cares for a working example:对于任何关心工作示例的人:

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

HTML HTML

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

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

Or if you prefer, we can wrap it in a function call it when an event occurs:或者,如果您愿意,我们可以将其包装在 function 中,并在事件发生时调用它:

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

HTML HTML

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

JS 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM