简体   繁体   English

如何显示带有附加到对象的源的图像? 对象(问题)是随机选择的,图像应随它们而变化

[英]How do you display an image with a source attached to an object? The object (question) is selected randomly and images should change with them

I have an array of objects which are questions.我有一系列问题的对象。 This is for a random question quiz.这是一个随机问题测验。 Basically, when one question is randomly selected, I thought all of the attributes would go with it (if "called" correctly).基本上,当随机选择一个问题时,我认为所有属性都会随之而来(如果“调用”正确)。 I am not sure how to display the image from the object when the question text (q) displays.当问题文本 (q) 显示时,我不确定如何显示来自对象的图像。

const questionBank = [
{
    q: "This is question 1's text",
    options:['1','2','3','4', '5'],
    answer:4,
    imgURL: 'http://drive.google.com/uc?export=view&id=googleDriveId',
},
{
    q: "question 2 will go here",
    options:['7','6','4'],
    answer:0,
    imgURL: 'http://drive.google.com/uc?export=view&id=differentDriveId',
}

function getNewQuestion(){
    const question = document.querySelector(".question");

    //set question text
    question.innerHTML = currentQuestion.q;

    //add image to question 
    //document.querySelector('.question').appendChild(img); (attempted, did not work)
    //var img = new Image(); (attempted, did not work)
    //img = document.createElement("img");  (attempted, did not work)
    //img.src = document.querySelector('.imgUrl'); (attempted, did not work)

I think that is all the relevant code.我认为这就是所有相关的代码。

You were on the right track with creating a new img element, if you take a look at this post, you need to create a new image element.您在创建新的 img 元素方面走在了正确的轨道上,如果您看一下这篇文章,您需要创建一个新的图像元素。

on your last line, instead set image.src = currentQuestion.imgURL as you want the current questions imgURL property, then you can appendChild() your new image element!在你的最后一行,设置image.src = currentQuestion.imgURL作为你想要的当前问题 imgURL 属性,然后你可以appendChild()你的新图像元素!

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

相关问题 如何在mousemove上随机更改图像源? - How do I randomly change images source on mousemove? 如何在随机选择的对象内创建随机属性? - How do I create a random property inside a randomly selected object? 如何从 object 的数组中仅随机显示一张 1 图像 - how to randomly display only one 1 image from the array of object 使用 Puppeteer,您将如何从网站上抓取标题和图像,并将它们放在同一个 object 中,以便图像与标题相关? - Using Puppeteer, how would you scrape a website for titles and images and have them be in the same object so that the image is related to the title? 使用Jquery,如何更改要单击的对象的CSS? - Using Jquery, how do you change the css of the object you are clicking? 如何通过来自Web来源的图像拆分和成像,并通过纹理将它们应用于threejs对象? - How to split and image through an image coming from a web source and apply them through a texture on a threejs object? 如何从随机选择的对象中选择随机索引? - How to select an random index from randomly selected object? 如何使用Flux Stores更改对象值? - How do you change Object values with Flux Stores? 如何将图像文件放在json对象中? - How do you put an image file in a json object? React-如何将本地图像导入/来源到对象 - React - How to import/source local images into an Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM