简体   繁体   English

我正在尝试将 append 图像从 javascript object 数组放到我的 html 页面上

[英]I'm trying to append an image from a javascript object Array onto my html page

So I was able to create a movie class array with 4 constructors, and a for loop with 3 iterations that represent three different movies.所以我能够创建一个电影 class 数组,其中包含 4 个构造函数,以及一个包含 3 次迭代的 for 循环,代表三部不同的电影。 On the html page this allows the user to open a dropdown selection and pick one of the three movies.在 html 页面上,这允许用户打开下拉选择并从三部电影中选择一部。 After hitting the submit button I want to display all the info for all the movie they selected including its image which is the difficult part.点击提交按钮后,我想显示他们选择的所有电影的所有信息,包括其图像,这是困难的部分。 Here is my code so far:到目前为止,这是我的代码:

<!DOCTYPE html>
 <html>
 <head>
 <script src="jquery.js"></script>
 <script src="Script.js"></script>
</head>
<body>
<form > 
<select> 
</select>
<button onclick="showDetail()">Submit</button>
</form>
<p></p>
</body>

 </html>


 class movie {
name;
price;
image;
genre;
constructor(x,y,z,a){
this.name = x;
this.price = y;
this.image = z;
this.genre = a;
   }
  }


 let movies =[];
movies[0]=  new movie("Deadpool", "$6.95","Image/Deadpool.jpg", "Superhero, Action");
movies[1]=  new movie("Titanic", "$4.95", "" , "Romance, Drama");
movies[2]=  new movie("Spiderman 3", "$6.95","", "Superhero, Action");



$(document).ready(function() { console.log( "ready!");
for (var i = 0; i < 3; i++){
movies[0].image= "Image/Deadpool.jpg";
movies[1].image= "Image/Titanic.jpg"; 
movies[2].image= "Image/Spiderman.jpg";
$("select").append(`<option>${movies[i].name}</option>`); console.log(i) }});

 function showDetail(){
 for (var i = 0; i < 3; i++){
    $("p").append(`${movies[i].price},${movies[i].genre}, ${movies[i].image}`);

   }
  }

You can append HTML using.append() but I would not recommend appending it to a paragraph tag, and using the paragraph selector, try doing this:你可以 append HTML using.append() 但我不建议将它附加到段落标记,并使用段落选择器,尝试这样做:

<div id="details"> </div>

Add the above instead of the paragraph tag and then in jquery you could so something like:添加上面的内容而不是段落标签,然后在 jquery 中你可以这样:

$('#details').append(`<h1> ${movies[i].price} </h1> <p> ${movies[i].genre} </p> <img src="${movies[i].image}" alt="movie" />

暂无
暂无

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

相关问题 在 JavaScript 中使用 fetch() 时,如何将返回的 object 中的详细信息渲染到 html 页面上 - When using fetch() in JavaScript how do I render details from the returned object onto the html page Javascript未将图像对象从数组加载到画布上 - Javascript not loading image object from array onto canvas 我正在尝试从我的数据库中显示图像,但图像不会填充img html属性的src - I'm trying to display an image from my database, but the image wont populate the src of the img html attribute I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object - I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object 即使我从我的 javascript 函数返回一个数组,我也得到一个对象 - I'm getting an object even though I'm returning an array from my javascript function 我正在尝试在 javascript 中将对象转换为对象数组 - I'm trying to convert Object to Array of object in javascript 我试图将数组的值从目标C中的.m文件发送到HTML或Javascript - I am trying to send the values of array from .m file in objective C to Html or Javascript 我正在尝试访问数组内部对象中的图像 - I'm trying to access an image that is in an object inside of an array HTML JavaScript 从文件打印到页面上 - HTML JavaScript print from file onto page 我想在没有jQuery的情况下从javascript附加html - I want to append my html from javascript without jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM