简体   繁体   English

如何通过从 JavaScript 中的数组对象生成数据来更新 HTML 中的内容?

[英]How to update content in HTML by generating data from array objects in JavaScript?

I am new to JavaScript and hope that I can get some guide here.我是 JavaScript 的新手,希望我能在这里得到一些指导。 So, my task is to update the content in a HTML block when a corresponding option is selected.因此,我的任务是在选择相应选项时更新 HTML 块中的内容。
I need to use 2 functions in the javascript given to generate the content from the javascript containing object arrays.我需要在 javascript 中使用 2 个函数来从包含 object ZA3CBC3F9D0CE2F2C1554E1B6719Z 的 javascript 生成内容。
I have set up the codes for selecting an option from the dropbox and it works well.我已经设置了从保管箱中选择选项的代码,并且效果很好。 The problem is I need to update the info ( movie's name from movies.js, the datetime and house ) of the corresponding cinema.问题是我需要更新相应电影院的信息(movies.js 中的电影名称、日期时间和房子)。 Whenever I choose a cinema, the contents have to be changed according to the cinema selected.每当我选择电影院时,必须根据所选电影院更改内容。 I tried to run a loop and it really seems stupid and failed as expected.我试图运行一个循环,它看起来真的很愚蠢并且如预期的那样失败了。 How can I edit my codes so that the contents can be updated when a corresponding option(cinema) is selected?如何编辑我的代码,以便在选择相应的选项(电影)时更新内容? THANK YOU for your time and your helps are much appreciated.感谢您的宝贵时间,非常感谢您的帮助。 I am stuck at this step for a really long time:(我在这一步卡了很长时间:(

This is my code (selecting a related cinema) and I tried to run the loop but failed:(:这是我的代码(选择一个相关的电影院),我试图运行循环但失败了:(:

<div id="Cinema">
<form id="cin" method="get" action="">
    <fieldset>
        <legend>Cinema</legend>
            <select id="cinemaSelect">
                <option value="default"></option>   
            </select> 
            <script>
                var select = document.getElementById("cinemaSelect");
                for(var i=0; i < cinemas.length; i++){
                    var option = document.createElement("OPTION"),
                    txt = document.createTextNode(cinemas[i].branchName);
                        option.appendChild(txt);
                    select.insertBefore(option,select.lastChild);
                    }
            </script>
        </fieldset>  
</form> 
</div>
<div id="lm">
<script>       
        function getMovieName(movieArray, id) {             
            for (let i=0; i<movieArray.length; i++) {                 
                if (movieArray[i].id == id) return movieArray[i].name;             
                }             
                return undefined;         
            }                     
            for (let i=0; i<cinemas.length; i++) {    
                let selected = document.getElementById("cinemaSelect").value;
                if (selected == cinemas[i].branchName){         
                    for (let j=0; j<cinemas[i].movies.length; j++) { 
                        let img = document.createElement("img");
                        img.src= getMovieName(movies, cinemas[i].movies[j].id).thumbnail;
                        document.getElementById("lm").appendChild(img);
                        let aaa = document.createElement("P");
                        aaa.innerHTML = getMovieName(movies, cinemas[i].movies[j].id);
                        document.getElementById("lm").appendChild(aaa);                                         
                            for (let k=0; k<cinemas[i].movies[j].shows.length; k++) { 
                                let bbb = document.createElement("P");                   
                                bbb.innerHTML = "datetime  " + cinemas[i].movies[j].shows[k].datetime + "<br/>" + "House" + cinemas[i].movies[j].shows[k].house;               
                                document.getElementById("lm").appendChild(bbb);                                     
                            }  
                    }                   
                }         
            }  
    </script> 
</div>

For cinema.js, the array given is:对于cinema.js,给出的数组是:

function getCinemas() {
    return [
        {
            branchName:"City Cinema - New York",
            movies:[
                {   id:1,
                    shows:[
                    {   
                        index:1,
                        datetime:"8 Nov, Sun - 9:00am",
                        house:3            
                    },
                    {   
                        index:2,
                        datetime:"10 Nov, Tue - 11:00am",
                        house:3            
                    }
                    ]
                },
                {   id:2,
                    shows:[
                    {   
                        index:3,
                        datetime:"9 Nov, Mon - 2:00pm",
                        house:1            
                    }
                    ]
                }
            ]
        },
        {
            branchName:"City Cinema - Los Angeles",
            movies:[
                {
                    id:2,
                    shows:[
                        {
                            index:4,
                            datetime:"11 Nov, Wed - 9:00am",
                            house:4
                        },
                        {
                            index:5,
                            datetime:"11 Nov, Wed - 11:00am",
                            house:2
                        }
                    ]
                }
            ]
        },
        {
            branchName:"City Cinema - Wisconsin",
            movies:[
                {
                    id:1,
                    shows:[
                        {
                            index:6,
                            datetime:"11 Nov, Wed - 9:00am",
                            house:4
                        }
                    ]
                }
            ]
        }
    ]
} 

For movies.js, the array given is:对于movies.js,给定的数组是:

function getMovies() {
    return  [
        {
            id:1,
            type:"now",
            thumbnail:"../xx.png",
            src:"../yy.mp4",
            name:"Casablanca",
            cast:"Humphrey Bogart, Ingrid Bergmen",
            director:"Michael Curtiz",
            duration: 120
        },
        {
            id:2,
            type:"now",
            thumbnail:"../xx.png",
            src:"../xx.mp4",
            name:"Chocalate",
            cast:"Christiano Roland, Wayne Beckman",
            director:"Alex Curtis",
            duration: 110
        },
        {
            id:3,
            type:"now",
            thumbnail:"../xx.png",
            src:"../xx.mp4",
            name:"Wildlife",
            cast:"Marcus Randford",
            director:"David De Geek",
            duration: 100
        },
        {
            id:4,
            type:"upcoming",
            thumbnail:"../xx.png",
            src:"../xx.mp4",
            name:"Barbecue",
            cast:"Taylor Watson",
            director:"Rolando Broom",
            duration: 105
        },
        {
            id:5,
            type:"upcoming",
            thumbnail:"../xx.png",
            src:"../xx.mp4",
            name:"BigBuck",
            cast:"Mark Hughes",
            director:"Tim Carlton",
            duration: 115
        },
        {
            id:6,
            type:"upcoming",
            thumbnail:"../xx.png",
            src:"../xx.mp4",
            name:"Nature",
            cast:"Benjamin Crook",
            director:"Memphis Delan",
            duration: 100
        }
    ]
} 

Have to learn some JS tricks...必须学习一些JS技巧...

 const Cinemas = [ { branchName: "City Cinema - New York", movies: [ { id: 1, shows: [ { index: 1, datetime: "8 Nov, Sun - 9:00am", house: 3 }, { index: 2, datetime: "10 Nov, Tue - 11:00am", house: 3 } ] }, { id: 2, shows: [ { index: 3, datetime: "9 Nov, Mon - 2:00pm", house: 1 } ] } ] }, { branchName: "City Cinema - Los Angeles", movies: [ { id: 2, shows: [ { index: 4, datetime: "11 Nov, Wed - 9:00am", house: 4 }, { index: 5, datetime: "11 Nov, Wed - 11:00am", house: 2 } ] } ] }, { branchName: "City Cinema - Wisconsin", movies: [ { id: 1, shows: [ { index: 6, datetime: "11 Nov, Wed - 9:00am", house: 4} ] } ] } ] const Movies = [ { id: 1, type: "now", thumbnail: "../xx.png", src: "../yy.mp4", name: "Casablanca", cast: "Humphrey Bogart, Ingrid Bergmen", director: "Michael Curtiz", duration: 120 }, { id: 2, type: "now", thumbnail: "../xx.png", src: "../xx.mp4", name: "Chocalate", cast: "Christiano Roland, Wayne Beckman", director: "Alex Curtis", duration: 110 }, { id: 3, type: "now", thumbnail: "../xx.png", src: "../xx.mp4", name: "Wildlife", cast: "Marcus Randford", director: "David De Geek", duration: 100 }, { id: 4, type: "upcoming", thumbnail: "../xx.png", src: "../xx.mp4", name: "Barbecue", cast: "Taylor Watson", director: "Rolando Broom", duration: 105 }, { id: 5, type: "upcoming", thumbnail: "../xx.png", src: "../xx.mp4", name: "BigBuck", cast: "Mark Hughes", director: "Tim Carlton", duration: 115 }, { id: 6 // <--, type: "upcoming", thumbnail: "../xx.png" // <--, src: "../xx.mp4", name: "Nature", cast: "Benjamin Crook", director: "Memphis Delan", duration: 100 } ] const DomParser = new DOMParser(), cinemaSlct = document.getElementById("cinemaSelect"), div_lm = document.querySelector('div#lm') Cinemas.forEach((cinema,index) => { cinemaSlct.add( new Option( cinema.branchName, index )) }); cinemaSlct.onchange = () => { div_lm.innerHTML = "" Cinemas[cinemaSlct.value].movies.forEach(movieC=> { movieC.shows.forEach(show=> { let movie = Movies.find(x=>x.id===show.index) if (.movie) throw 'no movie reference' let newSection = ` <section> <hr> <img src="${movie.thumbnail}" alt="${movie:name}"> <p>name. ${movie:name} <br> cast. ${movie:cast} <br> director. ${movie:director} </p> <hr> <p> datetime.${show:datetime} <br> house.${show;house} </p> <hr> </section>`. div_lm.appendChild( (DomParser,parseFromString( newSection. 'text/html')).body.firstChild ) }) }) }
 <div id="Cinema"> <form id="cin" method="get" action=""> <fieldset> <legend>Cinema</legend> <select id="cinemaSelect"> <option value="" selected disabled> select one</option> </select> </fieldset> </form> </div> <div id="lm"></div>

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

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