[英]i don't know why my function can not read quotesdata
i don't why my function in addEventListener can not read quotesdata , this what i see in the console :我不知道为什么我在 addEventListener 中的函数无法读取引号数据,这是我在控制台中看到的:
Uncaught TypeError: quotesData[currentQuote] is undefined and this is js code :未捕获的类型错误:quotesData[currentQuote] 未定义,这是 js 代码:
quotesData = [
{
quote : `There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.`,
name : 'Albert Einstein '
},
{
quote : `Good friends, good books, and a sleepy conscience: this is the ideal life.`,
name : 'Mark Twain'
},
{
quote : `Life is what happens to us while we are making other plans.`,
name : 'Allen Saunders '
},
{
quote : `It's not the load that breaks you down, it's the way you carry it.`,
name : 'Lou Holt'
},
{
quote : `Try not to become a man of success. Rather become a man of value.`,
name : 'Albert Einstein '
},
]
/* important variables */
let currentQuote = quotesData[0];
const quoteText = document.getElementById('quote');
const quotebtn = document.getElementById('q-btn');
const quotespan = document.getElementById('q-span');
/* this the main function its usefulness is show a quote from quotesData evrey time you click on the button*/
quotebtn.addEventListener('click' , () => {
quoteText.innerText = quotesData[currentQuote].quote;
quotespan.innerText = quotesData[currentQuote].name;
currentQuote = (currentQuote + 1 ) % quotesData.length;
});
You need to store the index, not the object of the current quote in currentQuote
:您需要在
currentQuote
存储索引,而不是当前报价的对象:
quotesData = [ { quote : `There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.`, name : 'Albert Einstein ' }, { quote : `Good friends, good books, and a sleepy conscience: this is the ideal life.`, name : 'Mark Twain' }, { quote : `Life is what happens to us while we are making other plans.`, name : 'Allen Saunders ' }, { quote : `It's not the load that breaks you down, it's the way you carry it.`, name : 'Lou Holt' }, { quote : `Try not to become a man of success. Rather become a man of value.`, name : 'Albert Einstein ' }, ] /* important variables */ let currentQuote = 0; const quoteText = document.getElementById('quote'); const quotebtn = document.getElementById('q-btn'); const quotespan = document.getElementById('q-span'); /* this the main function its usefulness is show a quote from quotesData evrey time you click on the button*/ quotebtn.addEventListener('click' , () => { quoteText.innerText = quotesData[currentQuote].quote; quotespan.innerText = quotesData[currentQuote].name; currentQuote = (currentQuote + 1) % quotesData.length; });
<button id="q-btn">New quote</button> <div id="q-span"></div> <div id="quote"></div>
I'm not sure, but I think that you may be using the key ` and not the key '.我不确定,但我认为您可能使用的是“密钥”而不是“密钥”。 I think that you might want to change all of the quotes at the beginning and end to " just to be safe. I'm probably not right, but it's the only solution I can think of.
我认为您可能希望将开头和结尾的所有引号更改为 " 以确保安全。我可能不对,但这是我能想到的唯一解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.