[英]Using a data attribute to pass information to a modal: is there a way to pass more than one piece of information?
[英]Array with more than one piece of information - Javascript
我有這個javascript代碼:
var randomwordz = [
{word: 'Sweets', image: 'sweets.jpg'},
{word: 'Chocolate', image: 'chocolate.jpg'},
{word: 'Snow', image: 'snow.jpg'},
{word: 'Pineapple', image: 'pineapple.gif'},
{word: 'Juice', image: 'j.jpg'},
{word: 'Egg', image: 'egg.gif'},
{word: 'Cheese', image: 'cheese.gif'},
{word: 'Chicken', image: 'chick.jpg'},
{word: 'Veg', image: 'veg.gif'}];
randomWords[0].word
randomWords[0].image
randomWords[1].word
randomWords[1].image
randomWords[2].word
randomWords[2].image
randomWords[3].word
randomWords[3].image
randomWords[4].word
randomWords[4].image
randomWords[5].word
randomWords[5].image
randomWords[6].word
randomWords[6].image
randomWords[7].word
randomWords[7].image
randomWords[8].word
randomWords[8].image
var randoms = randomWords[Math.floor(Math.random()*randomWords.length)];
document.getElementById('randomwords').innerHTML = randoms;
我的隨機單詞顯示在我的頁面上,但是自從我在顯示隨機單詞時向數組添加圖像后,它就會顯示-[object Object]而不是我的隨機單詞。
有誰知道為什么和如何只顯示隨機單詞。 我需要單詞和圖像配對以用於代碼的下一部分。
不好意思我對Javascript不夠了解
您有一個對象 數組 ,現在需要在選擇索引后從對象中選擇所需的屬性
document.getElementById('randomwords').innerHTML = randoms.word;
// and you may want to do something like this for your image
document.getElementById('randomimages').src= randoms.image;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.