您好,我是 HTML5 和 j 查询的新手。我必须从我成功完成的 XML 中解析数据。现在解析数据后,我想使用 j 查询随机显示它。请建议我该怎么做。
代码片段是
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/multiquestions1.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("question");
$(document).ready(function (){
for(var j=0;j<x.length;j++)
{
if(j==0)
{
$("#navigationlist").append('<li><a href="#" id="selected_link" class="navg" onClick="display_nav('+j+',this);">'+(j+1)+'</a></li>');
display_nav(j,$("#selected_link"))
}
else
$("#navigationlist").append('<li><a href="#" class="navg" onClick="display_nav('+j+',this);">'+(j+1)+'</a></li>');
}
});
function display()
{
// for fetch question
Description=(x[i].getElementsByTagName("questionTxt")[0].childNodes[0].nodeValue);
Answer =(x[j].getElementsByTagName("image")[0].childNodes[0].nodeValue);
document.getElementById('options').style.backgroundImage = 'url('+Answer+')';
// Assign the value
document.getElementById("question").innerHTML= Description;
}
function display_nav(j,obj)
{
i=j;
$("#feedback").html("");
$(".navg").each(function(){
$(this).removeAttr("id");
});
$(obj).attr("id","selected_link");
Description=(x[j].getElementsByTagName("questionTxt")[0].childNodes[0].nodeValue);
Answer =(x[j].getElementsByTagName("image")[0].childNodes[0].nodeValue);
answermin=(x[j].getElementsByTagName("answermin")[0].childNodes[0].nodeValue);
answermax=(x[j].getElementsByTagName("answermax")[0].childNodes[0].nodeValue);
$("#bob").attr("min",answermin);
$("#bob").attr("max",answermax);
$("#bob").attr("value",0);
$("#out").html(0);
$("#out").css({left:0});
// Assign the value
document.getElementById("question").innerHTML= Description;
//document.getElementById("options").style.backgroundImage = url(Answer);
document.getElementById('options').style.backgroundImage = 'url('+Answer+')';
}
and xml is
<?xml version="1.0" encoding="utf-8"?>
<FlashCards>
<question id="1">
<questionTxt type="text"><![CDATA[Hello!]]></questionTxt>
<image visible="true"><![CDATA[img/smiley1.png]]></image>
</question>
<question id="2">
<questionTxt type="text"><![CDATA[Hi]]></questionTxt>
<image visible="true"><![CDATA[img/smiley2.png]]></image>
</question>
</FlashCards>