我有一系列問題的物件。這是一個隨機問題測驗。基本上,當隨機選擇一個問題時,我認為所有屬性都會隨之而來(如果“呼叫”正確)。當問題文本 (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)
我認為這就是所有相關的代碼。
uj5u.com熱心網友回復:
您在創建新的 img 元素方面走在了正確的軌道上,如果您看一下這篇文章,您需要創建一個新的影像元素。
在你的最后一行,而不是設定image.src = currentQuestion.imgURL你想要的當前問題 imgURL 屬性,然后你可以appendChild()你的新影像元素!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483555.html
標籤:javascript 图片 目的 随机的
