![](/img/trans.png)
[英]Automatizing the questions and answers asked in Dialogflow from a json file that contains them
[英]Show random questions and answers from XML file
我正在尝试构建一个单页HTML应用程序,以按随机顺序显示问题和答案。
这是所需的功能:
因为我正在尝试实现类似用户体验的移动应用程序,所以我不希望在初始页面加载后发生任何页面重新加载,但是当用户点击按钮时,应立即显示“问答”。
我是否可以仅使用JavaScript / jQuery来完成此操作? 如果需要,我也可以使用PHP,但是纯JS实现会更有趣。
如果有人能给我一些解决方法的建议,我将不胜感激。
假设您的按钮具有“按钮”类,则在具有“问题”类的div上显示问题,并将xml保存在变量中,假设是问题,以下jQuery代码应该可以解决问题
var q;
$( '.button' ).click( function(){
if( q == false ){
q = $( questions ).sort( function(){
return Math.random() > 0.5
} ).first();
$( '.question' ).text( q.children( 'question' ).text() );
}
else{
$( '.question' ).text( q.children( 'answer' ).text() );
$( questions ).first().remove();
q = false;
}
} );
我假设您的xml如下所示:
<entry>
<question>Here lies a question.</question>
<answer>And here is the answer!</answer>
</entry>
<entry>
<question>Here lies another question.</question>
<answer>And here is the answer!</answer>
</entry>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.