小弟我要用js从json文件中取值,并输出的html页面,有个有关问题
我要用js从json文件中取值,并输出的html页面,有个问题
json文件有多个 0.json 1.json 2.json 3.json ...
现在的情况是js可以挨个读取json文件中的内容,但是每次读取新文件的时候之前的文件内容就没了,我想要加载新json文件内容时,保留之前的读出来的内容
求大神!!!
js 为:
var i = 0; //当前文件头部
window.onload = function () {
var host = window.location.host; //获取域名
function getData() {
//根据页数读取数据
var filename = i+".json";
i++;
$.getJSON("http://bendi.ceshi.com/"+filename, function (data) {
var $content = $("#content");
var html ='';
// $jsontip.empty();//清空内容
$.each(data, function (infoIndex, info) {
html += '<ul>';
html += '<li>';
html += '<a href=\"'+ host +'/'+ info['url'] + '\" target=\"_blank\">';
html += '<div class="Conpic"><img src="' + info["cover"] + '" width="100%" ></div>';
html += '<div class="Contxt">';
html += '<h3>' + info["title"] + '</h3>';
html += '<span class="Confun"></span></div>';
html += '</a></li>';
html += '</ul>';
})
$content.html(html);//显示处理后的数据
})
}
getData();
function scrollHandler() {
$("#pullUpB").show();
$("#pullUpA").hide();
}
$("#pullUpB").click(function () {
getData();
scrollHandler();
});
}
------解决思路----------------------
你是覆盖了,应该使用追加。
$content.html(html);//显示处理后的数据
改为
$content.append(html);//显示处理后的数据
------解决思路----------------------
$content.html(html);//显示处理后的数据 遍历一次,把之前的东西覆盖掉了
追加使用append
json文件有多个 0.json 1.json 2.json 3.json ...
现在的情况是js可以挨个读取json文件中的内容,但是每次读取新文件的时候之前的文件内容就没了,我想要加载新json文件内容时,保留之前的读出来的内容
求大神!!!
js 为:
var i = 0; //当前文件头部
window.onload = function () {
var host = window.location.host; //获取域名
function getData() {
//根据页数读取数据
var filename = i+".json";
i++;
$.getJSON("http://bendi.ceshi.com/"+filename, function (data) {
var $content = $("#content");
var html ='';
// $jsontip.empty();//清空内容
$.each(data, function (infoIndex, info) {
html += '<ul>';
html += '<li>';
html += '<a href=\"'+ host +'/'+ info['url'] + '\" target=\"_blank\">';
html += '<div class="Conpic"><img src="' + info["cover"] + '" width="100%" ></div>';
html += '<div class="Contxt">';
html += '<h3>' + info["title"] + '</h3>';
html += '<span class="Confun"></span></div>';
html += '</a></li>';
html += '</ul>';
})
$content.html(html);//显示处理后的数据
})
}
getData();
function scrollHandler() {
$("#pullUpB").show();
$("#pullUpA").hide();
}
$("#pullUpB").click(function () {
getData();
scrollHandler();
});
}
------解决思路----------------------
你是覆盖了,应该使用追加。
$content.html(html);//显示处理后的数据
改为
$content.append(html);//显示处理后的数据
------解决思路----------------------
$content.html(html);//显示处理后的数据 遍历一次,把之前的东西覆盖掉了
追加使用append