在没有服务器的情况下将XML导入HTML [英] Importing XML to HTML Without a Server

查看:51
本文介绍了在没有服务器的情况下将XML导入HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在不使用服务器的情况下将XML文档导入HTML.我正在使用独立计算机,因此无法将其上传到任何服务器,这意味着我无法使用XMLHttpRequest.我在这里找到一个线程说可以使用jQuery AJAX,但事实证明,我也不能使用它.有人知道我可以在不将整个XML复制到JS代码的情况下做到这一点的方法吗?

I've been trying to import an XML document into HTML without using a server. I'm working with standalone computers so I can't upload it to any server, which means I can't use XMLHttpRequest. I found a thread here saying I could use jQuery AJAX, but as it turns out, I can't use that either. Does anybody know a way I could do it without copying the entire XML into the JS code?

对于请求,这是我需要做的事情:基本上,XML包含我要从中创建测试的问题.现在,该测试并不包含所有问题,因此我使用JS来挑选随机问题并将其放置在HTML文档中.我不想将整个XML复制到脚本文件,因为它看起来确实很不专业,并且我想使其尽可能简单.

To requests, here's what I need to do with it: Basically the XML contains questions from which I want to create a test. Now, the test doesn't include all the questions, so I'm using JS to pick random questions and place them in the HTML document. I don't want to copy the entire XML to the script file because it looks really unprofessional and I want to keep it as simple as possible.

在这一点上,我真的愿意尝试任何东西,包括JSON,如果这样做更容易的话.我只需要能够在数据上使用JS并将其写在HTML文档中的能力.

At this point I'm really willing to try anything, including JSON, if that makes it easier. I just need the ability to use JS on the data and the ability to write it in the HTML document.

推荐答案

一种选择是改为将问题文件包含为JavaScript内容(仍在单独的文件中).

One option would be to include the question file as JavaScript content (still in a separate file) instead.

如果内容真的很简单(例如,只是一个列表),那么您的文件可能只是一个简单的数组.

If the content is really simple (eg just a list) then your file could just be a simple array.

var questions = [
  "What time is it?",
  "Is the sky blue?",
  "What do you call a fish with no legs?"
];

您只需将此文件包含在脚本标签中即可:

You just include this file in your page with a script tag:

<script src="questions.js"></script>

如果您的问题结构更复杂,则可以将其以JSON格式包含,从而可以进行复杂的嵌套等.

If your question structure is more complicated you can include it in a JSON format that allows for complex nesting etc.

var questions = [
  {
    "id":1,
    "question":"What time is it?",
    "datatype":"text"
  },
  {
    "id":2,
    "question":"Is the sky blue?",
    "datatype":"boolean"
  },
  {
    "id":3,
    "question":"What do you call a fish with no legs?",
    "datatype":"text"
  }
];

无论哪种方式,一旦将问题数据加载到内存中,您都可以将问题随机化并确定要呈现的问题.

Either way once the question data is loaded into memory you can randomize the questions and decide which ones to render.

这篇关于在没有服务器的情况下将XML导入HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆