Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一次在DOM中维护五个部分_Javascript_Json - Fatal编程技术网

Javascript 一次在DOM中维护五个部分

Javascript 一次在DOM中维护五个部分,javascript,json,Javascript,Json,我正在创建电子书阅读器,现在我想从链接获取电子书的部分: http://tmaserv.scem.uws.edu.au/chapters/?n=0 It will fetch Section number 0 http://tmaserv.scem.uws.edu.au/chapters/?n=1 It will fetch Section number 1 .. so on 可以从url获取节数 现在,我可以获取一个节,但我想要的是,读者应该随时在DOM中维护文档的5个节,而

我正在创建电子书阅读器,现在我想从链接获取电子书的部分:

 http://tmaserv.scem.uws.edu.au/chapters/?n=0   It will fetch Section number 0
 http://tmaserv.scem.uws.edu.au/chapters/?n=1   It will fetch Section number 1
 ..
 so on
可以从url获取节数

现在,我可以获取一个节,但我想要的是,读者应该随时在DOM中维护文档的5个节,而且我们一次只能获取一个节。前5个部分将被提取并加载文档,然后根据请求提取其他部分

代码:


电子书籍
var-requestNum=0;
//假设我获取了varibale中的节数,比如sectionCount
函数do_练习(){
var x=新的XMLHttpRequest();
//如前所述,调整GET URL以反映新的n值和请求
x、 打开('获取','http://tmaserv.scem.uws.edu.au/chapters/?n=“+requestNum,true);
x、 onreadystatechange=函数(){
如果(x.readyState==4&&x.status==200){
obj=(x.responseText);
parse(x.responseText);
obj=JSON.parse(obj);
document.getElementById(“section1”).innerHTML=obj.data;
requestNum++;
}
}
x、 发送(空);
}
下一节

我是否应该使用一些数组来获取5个部分并维护它们?

您是否正在尝试这样做

var cacheSize=5;
var=[];
var-requestNum=0;
函数fetchChapter(){
var x=新的XMLHttpRequest();
//如前所述,调整GET URL以反映新的n值和请求
x、 打开('获取','http://tmaserv.scem.uws.edu.au/chapters/?n='+requestNum++,true);
x、 onreadystatechange=函数(){
如果(x.readyState==4&&x.status==200){
obj=(x.responseText);
parse(x.responseText);
obj=JSON.parse(obj);
章节推送(对象数据);
如果(chapters.length>cacheSize){
第三章:移位();
}
对于(变量i=0;i
您说的是DOM,但您的代码都是Javascript,所以您是指通过Javascript在客户端内存中还是要将其全部加载到DOM中。@Leeish我需要将其加载到DOM中
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EBook</title>
<script>
var requestNum = 0;
// assume I had fetched the number of sections in varibale say sectionCount
function do_exercise () {
    var x = new XMLHttpRequest();
    // adjust the GET URL to reflect the new n value and request as before
    x.open('GET', 'http://tmaserv.scem.uws.edu.au/chapters/?n=' + requestNum, true);
    x.onreadystatechange = function() {
        if (x.readyState == 4 && x.status ==200) {
            obj = (x.responseText);
            JSON.parse(x.responseText);
            obj = JSON.parse(obj);
            document.getElementById("section1").innerHTML = obj.data;
            requestNum++;
        }
    }

    x.send(null);
}
</script>
<body>
<nav>           
    <button onclick="do_exercise();">Next section</button>      
</nav>
<section id = "section1">
</section>
</body>
</html>