我正在Django上創建一個網站,向學生提供拼寫測試,然后他們填寫該分數,然后得到10分。不允許學生看到這些單詞,因此我使用功能'給了他們一系列聲音字節說“ speak()”,只有當我按下按鈕時,它們都說相同的話,因為我不知道如何將單個數據解析到函數中,以告訴它“說”哪個拼寫。我知道它不起作用的原因是因為我正在將“ sBite.id”解析為speak()函數,而sBite.id始終是相同的值。我只是不知道我應該解析什么值才能將正確的拼寫發送給函數以下是相關的JS代碼:function speak(word){ var msg = new SpeechSynthesisUtterance(usedWords[word]); window.speechSynthesis.speak(msg);}usedWords=[]; function load(played, mistake, score){ alert(mistake) var x =["ball","racket","apple","cheese","find","any","new", "display","please","happy","plead","pleat","aple","bple","cple"]; var step; var spellList = document.getElementById("spellList"); if(listlen < 10){ for(step = 0;step < 10;step++){ li = document.createElement("li"); sBite = document.createElement("button"); rand = x[Math.floor(Math.random() * x.length)]; if(usedWords.includes(rand)){ step = step - 1; continue; } else{ usedWords.push(rand); li.appendChild(document.createTextNode(rand)); li.id = "spelling"; spellList.appendChild(li); var testy = usedWords[step]; sBite.setAttribute("onClick","speak(sbite.id)"); sBite.type="button"; sBite.id=step; sBite.textContent=sBite.id; spellList.appendChild(sBite); listlen++; }
如何在JavaScript中創建一系列可以單擊以執行同一功能的不同版本的按鈕?
一只名叫tom的貓
2021-04-09 16:15:27