EDITED: Ok, ich habe nicht gut erklären. Ich habe eine Schaltfläche im HTML hinzugefügt. Wenn Sie darauf klicken, wird eine Warnung angezeigt und der Benutzer aufgefordert, Fragen und Antworten einzugeben. ABER ... es bringt dieses Array nicht in die Karten selbst.Wie starte ich diesen Javascript-Code auf klicken
Ich habe zusammen ein einfaches Javascript-Flash-Karten-Programm gehackt. Aber es beginnt sofort beim Laden der Seite. Wie kann ich es auf einen Klick <button>
starten? Ich habe versucht, den gesamten Code in eine Funktion einzuschließen, aber dann werden die Benutzereingaben, die das Array erzeugen, nicht an die Flashcards weitergegeben - ich nehme an, weil sie getrennte Funktionen sind. Ich werde das wahrscheinlich nicht gut erklären. Aber ich möchte, dass das gesamte Programm auf Knopfdruck läuft. Ich schätze jede Hilfe.
#flashcardFront {
margin-top: 100px;
margin-left: 400px;
width: 300px;
height: 50px;
background-color: black;
color: white;
font-family: arial;
font-size: 22px;
text-align: center;
padding: 50px 0;
display: block;
}
a:link {
text-decoration: none;
}
#number {
color: red;
position: relative;
left: -120px;
top: 30px;
}
<div>
<button onclick='cards();'>button</button>
<a id="flashcardFront" href="#" onclick="flashCards();"></a>
</div>
var myArray = [];
for (var i = 0; i < 2; i++) { // # of loops
myArray.push(prompt('Enter question ' + (i+1))); // push the value into the array
myArray.push(prompt('Enter answer ' + (i+1))); // push the value into the array
}
/*var myArray = [
"Q: What's my name?", 'A: Heck no.',
'Q: My age?', "A: Cool kids don't say.",
'Q: Fave rocker?', 'A: Paul Gilbert'
];*/
var myIndex = 0;
function renderQuestion(index) {
// render number if index is even
var str = myArray[index]
if (index % 2 == 0) {
str += '<br><span class="question-number">' + (index/2 + 1) + '</span>'
}
return str
}
function flashCards() {
var flashcardFront = document.getElementById('flashcardFront');
flashcardFront.innerHTML = renderQuestion(myIndex);
myIndex = (myIndex + 1) % (myArray.length);
}
flashCards()
@Roizpi Semikolons sind nicht wirklich erforderlich - [ASI] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion). –