使用jQuery和HTML对XML数据进行分页

时间:2022-01-18 15:44:41

I've got an XML file that contains a list of questions. I'd like to load the first question in the list when an HTML page loads and load the answers as radio buttons. When one of the radio buttons is selected, I'd like to display the results as well as a continue button. The continue button would go to the second element in the XML file.

我有一个包含问题列表的XML文件。当HTML页面加载并加载答案作为单选按钮时,我想加载列表中的第一个问题。当选择其中一个单选按钮时,我想显示结果以及继续按钮。 continue按钮将转到XML文件中的第二个元素。

So, I've got the following thus far:

所以,到目前为止,我有以下内容:

function generateQuestion(i) {
    $(document).ready(function() {
        $.get('quiz.xml', function(d) {
            alert('Loaded XML');
            $(d).find('question').get(0, function() {
                alert('Loaded Question');
                var $question = $(this);
                var questionContent = $question.attr("content");
                $(questionContent).appendTo("#quizQuestion");
            });
        });
    });
}

However, the content from the question is never loaded in the element. It looks like it hangs when I go to get the first element in the document. My guess is that there is no overload to inject the object into the function.

但是,问题中的内容永远不会加载到元素中。当我去获取文档中的第一个元素时,它看起来像是挂起。我的猜测是没有重载将对象注入函数。

Am I correct? Anyone have any quick resources that shows a basic quiz-like example like I'm looking to generate?

我对么?任何人都有任何快速资源,显示像我想要生成的基本测验类似的例子?

UPDATE: I've updated the code to the following and it seems to get caught on the attribute property:

更新:我已经将代码更新为以下内容,它似乎陷入了属性属性:

function generateQuestion(i) {
    $(document).ready(function() {
        $.get('quiz.xml', function(d) {
            var $question = $(d).find('question').get(i);
            var questionContent = $question.attr('content');
            alert(questionContent);
            $(questionContent).appendTo("#quizQuestion");
        });
    });
}

Any ideas?

1 个解决方案

#1


the get() function returns the non-wrapped set element in this case it would be the xml element.

get()函数返回非包装的set元素,在这种情况下它将是xml元素。

$.get('quiz.xml', function(d) {
    var question = $(d).find('question :eq(0)');
    var questionContent = question.attr('content');
    alert(questionContent);
    $(questionContent).appendTo("#quizQuestion");
});

the other alternative is to re-wrap the object

另一种选择是重新包装对象

$.get('quiz.xml', function(d) {
    var question = $(d).find('question').get(0);
    var questionContent = $(question).attr('content');
    alert(questionContent);
    $(questionContent).appendTo("#quizQuestion");
});

Also, I'm not sure why your including the $(document).ready inside the function call. When are you calling the function. if its after the document load then you don't really need that.

另外,我不确定为什么你在函数调用中包含$(document).ready。你什么时候打电话给这个功能。如果它在文档加载后,那么你真的不需要它。

#1


the get() function returns the non-wrapped set element in this case it would be the xml element.

get()函数返回非包装的set元素,在这种情况下它将是xml元素。

$.get('quiz.xml', function(d) {
    var question = $(d).find('question :eq(0)');
    var questionContent = question.attr('content');
    alert(questionContent);
    $(questionContent).appendTo("#quizQuestion");
});

the other alternative is to re-wrap the object

另一种选择是重新包装对象

$.get('quiz.xml', function(d) {
    var question = $(d).find('question').get(0);
    var questionContent = $(question).attr('content');
    alert(questionContent);
    $(questionContent).appendTo("#quizQuestion");
});

Also, I'm not sure why your including the $(document).ready inside the function call. When are you calling the function. if its after the document load then you don't really need that.

另外,我不确定为什么你在函数调用中包含$(document).ready。你什么时候打电话给这个功能。如果它在文档加载后,那么你真的不需要它。

相关文章