简体   繁体   English

用JavaScript填充多个DIV

[英]Fill multiple DIVs with JavaScript

I am trying to create a definition block on my website, where it randomly picks a word from an array then displays it along with its IPA and definition. 我正在尝试在我的网站上创建一个定义块,它在其中从数组中随机选择一个单词,然后将其连同其IPA和定义一起显示。

I have a basic understanding of JavaScript, however this is really above me. 我对JavaScript有基本的了解,但是这确实对我重要。

I have written some really basic, psuedo code, to demonstrate what I'd like to do. 我已经写了一些非常基本的伪代码来演示我想做的事情。 Any help on how to actually do it would be greatly appreciated! 任何对实际操作的帮助将不胜感激!

JavaScript: JavaScript的:

var Selector = Random(3)
var Definitions = [
    {
        "word":"House",
        "phonetic":"haʊs",
        "definition":"The part of a theatre where the audience is seated, also known as an auditorium."
    },
    {
        "word":"Apron",
        "phonetic":"ˈeɪprən",
        "definition":"In a traditional theatre, the part of the stage which projects in front of the curtain. In many theatres this can be extended, sometimes by building out over the pit."
    }, 
    {
        "word":"Barn Door",
        "phonetic":"bɑːn dɔː",
        "definition":"An arrangement of four metal leaves placed in front of the lenses of fresnel spotlights (qv) to control the shape of the light beam."
    }
 ]

HTML HTML

<div id="word">{Definition.Selector.word}</div>
<div id="phonetic">{Definition.Selector.phonetic}</div>
<div id="definition">{Definition.Selector.definition}</div>

While I understand this is nothing like workable code, I hope it can give enough of a representation of my idea. 虽然我知道这不像是可行的代码,但我希望它能充分体现我的想法。

Which long story short is: Pick random number. 长话短说: 选择随机数。 Select item from array. 从数组中选择项目。 Fill three divs with three pieces of content from that array item. 用该数组项中的三个内容填充三个div。

Thanks for your time(and hopefully answers)! 感谢您的宝贵时间(并希望能得到答复)!

And, to do the same thing depperm did using pure JavaScript, see this jsFiddle . 并且,要完成depperm使用纯JavaScript所做的相同操作,请参阅此jsFiddle

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

var definitions = [
    {
        "word":"House",
        "phonetic":"haʊs",
        "definition":"The part of a theatre where the audience is seated, also known as an auditorium."
    },
    {
        "word":"Apron",
        "phonetic":"ˈeɪprən",
        "definition":"In a traditional theatre, the part of the stage which projects in front of the curtain. In many theatres this can be extended, sometimes by building out over the pit."
    }, 
    {
        "word":"Barn Door",
        "phonetic":"bɑːn dɔː",
        "definition":"An arrangement of four metal leaves placed in front of the lenses of fresnel spotlights (qv) to control the shape of the light beam."
    }
 ];

var index = getRandomInt(0, definitions.length);

document.getElementById('word').innerHTML = definitions[index].word;

document.getElementById('phonetic').innerHTML = definitions[index].phonetic;

document.getElementById('definition').innerHTML = definitions[index].definition;

One viable solution 一种可行的解决方案

 function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } var Definitions = [ { "word":"House", "phonetic":"haʊs", "definition":"The part of a theatre where the audience is seated, also known as an auditorium." }, { "word":"Apron", "phonetic":"ˈeɪprən", "definition":"In a traditional theatre, the part of the stage which projects in front of the curtain. In many theatres this can be extended, sometimes by building out over the pit." }, { "word":"Barn Door", "phonetic":"bɑːn dɔː", "definition":"An arrangement of four metal leaves placed in front of the lenses of fresnel spotlights (qv) to control the shape of the light beam." } ]; var rand=getRandomInt(0, Definitions.length); $('#word').text(Definitions[rand].word); $('#phonetic').text(Definitions[rand].phonetic); $('#definition').text(Definitions[rand].definition); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="word"></div> <div id="phonetic"></div> <div id="definition"></div> 

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

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