如何创建div并根据变化的变量为其提供ID?

时间:2014-11-10 19:04:22

标签: javascript jquery

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

然后一个函数接受这个数字并运行一个循环来为每个问题制作一个单独的问题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并将其循环时,我打破了它。

1 个答案:

答案 0 :(得分:3)

你说这部分代码不起作用:

$('#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
  • 的元素的最后一个子元素