gpt4 book ai didi

javascript - 如何创建一个 div 并根据不断变化的变量为其指定一个 ID?

转载 作者:行者123 更新时间:2023-11-29 19:32:14 25 4
gpt4 key购买 nike

我正在做一个测验。用户决定他想回答多少问题。

然后一个函数获取这个数字并运行一个循环来为每个问题创建一个单独的问题 div。测验显示一个汉字,用户必须选择正确的翻译。

我的代码:

var fillInQuestion = function() {
questionDivIdHTML = 'question' + questionNum;

/****************************************
How do I make this Div's ID = to questionDivIdHTML?


// Creates question div
$('#questionArea').append("<div class='questionDiv'></div>");
//$('#questionArea:last-child').attr("id", questionDivIdHTML); <-- NOT WORKING


***************************************/

// Sets up a choice bank.
var choices = [];

// choices will be chosen from this.
var tempAnswerSet = allChoices.slice(0);

//find random item in the database for the question
var thisQuestion = allQuestions[Math.floor(Math.random() * allQuestions.length)];

// add that item to choices
choices.push(thisQuestion);

// remove item from 'database' so it cannot be used in another question
allQuestions.splice(allQuestions.indexOf(thisQuestion), 1);

// remove item from tempAnswer set so it can only be one choice
tempAnswerSet.splice(tempAnswerSet.indexOf(thisQuestion), 1);

// add three more items from the database (incorrect items)
var i = 3;
for (i; i > 0; i--) {
var addChoice = tempAnswerSet[Math.floor(Math.random() * tempAnswerSet.length)];
choices.push(addChoice);
// remove the one selected each time so they cant be chosen again
tempAnswerSet.splice(tempAnswerSet.indexOf(addChoice), 1);
//console.log("choices length: " + choices.length);
}
// shuffle the array
choices.shuffle();

// fill in the div with choices.
$('#questionDivIdHTML').append("Here is an question prompt:" + thisQuestion.english + " <br>");
$('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[0].hanyu</script>'></input>" + choices[0].hanyu + "<br>");
$('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[1].hanyu</script>'></input> " + choices[1].hanyu + "<br>");
$('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[2].hanyu</script>'></input> " + choices[2].hanyu + "<br>");
$('questionDivIdHTMLwithHash').append("<input type='radio' name='question<script>questionNum</script>Choice' value='<script>choices[3].hanyu</script>'></input> " + choices[3].hanyu + "<br>");

};

var fillOutQuiz = function() {
for (questionAmount; questionAmount > 0; questionAmount--) {
fillInQuestion();
questionNum += 1;
}
};

我已经让这段代码正常工作,但我在尝试添加动态 ID 并循环它时破坏了它。

最佳答案

你是说这部分代码不工作:

$('#questionArea').append("<div class='questionDiv'></div>");
$('#questionArea:last-child').attr("id", questionDivIdHTML);

嗯,它不起作用,因为 :last-child 伪选择器使用不当(见下文)。应该是:

$('#questionArea').append("<div class='questionDiv'></div>");
$('#questionArea > :last-child').attr("id", questionDivIdHTML);

或者更好的是,您可以像这样重新安排您的代码:

$("<div class='questionDiv'></div>")
.attr("id", questionDivIdHTML)
.appendTo("#questionArea");

  • #questionArea:last-child 选择一个 id = questionArea 的元素,它也是其父元素的最后一个子元素
  • #questionArea > :last-child 选择 id = questionArea 的元素的最后一个子元素

关于javascript - 如何创建一个 div 并根据不断变化的变量为其指定一个 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com