javascript - jqery ajax問(wèn)題
問(wèn)題描述
我的問(wèn)題是這樣的
當(dāng)顯示了5條之后 點(diǎn)擊“繼續(xù)添加”鏈接時(shí),點(diǎn)一次添加指定數(shù)量的數(shù)據(jù),直到j(luò)son文件中的數(shù)據(jù)取完為止也就是 顯示了5條之后,第一次點(diǎn)了,顯示6,7,8,第二次顯示9,10,11, 以此類(lèi)推,有什么辦法解決下,我的代碼如下:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <script src='http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js'></script> <title>測(cè)試</title> <script src='http://m.cgvv.com.cn/web/js/jquery-3.2.1.min.js'></script> <script src='http://m.cgvv.com.cn/wenda/text.json'type='application/json'></script></head><body><p id='btn'>點(diǎn)擊</p><p id='json'></p><a href='javascript:void(0)' style='display: none'>繼續(xù)添加</a><p class='num'></p><p class='ccc'></p><script> $(document).ready(function () {$(’#btn’).click(function () { var json=$(’#json’); var num=0; var p=$(’p’); json.append(’<p class='json'></p>’) $.ajax({type:’post’,url:’text.json’,data:null,dataType:’json’,success:function (ress) { var str; str=’’; $.each(ress,function (index,res) {if(num<5){ str+=’<p class='lens'><span>名稱(chēng):’+res[’tip’]+’</span></br>’; str+=’<span>作者:’+res[’author’]+’</span></br></p>’; $(’.clicks’).css({display:’block’ }) $(’.num’).html(’已加載了’+(num+1)+’條數(shù)據(jù)....’);}json.html(str);num++; }); var count=0; $.each(ress,function (index,res) {if(index>4){ $(’.clicks:last’).click(function () {if(count<3){ str= ’<p class='lens'><span>’+res[’tip’]+’</span></br>’ +’<span>’+res[’author’]+’</span></br></p>’ $(’#json’).append(str )}count++ });} }) var str1; str1=ress.length-$(’.lens’).length; $(’.ccc’).html(’還有’+(str1-1)+’條數(shù)據(jù)’);},error:function (res) { console.log(res)} })}) })</script><style> .lens{border-bottom: 2px solid red}</style></body></html>
問(wèn)題解答
回答1:第一次請(qǐng)求,把返回的數(shù)據(jù)存到一個(gè)數(shù)組里,下次點(diǎn)擊的時(shí)候操作這個(gè)數(shù)組就行了,不用再次請(qǐng)求了
相關(guān)文章:
1. android - 用textview顯示html時(shí)如何寫(xiě)imagegetter獲取網(wǎng)絡(luò)圖片2. javascript - table列過(guò)多,有什么插件可以提供列排序和選擇顯示列的功能3. showpassword里的this 是什么意思?代表哪個(gè)元素4. javascript - windows下如何使用babel,遇到了困惑5. JavaScript事件6. python - 為什么正常輸出中文沒(méi)有亂碼,zip函數(shù)之后出現(xiàn)中文編程unicode編碼的問(wèn)題,我是遍歷輸出的啊。7. javascript - js中向下取整8. android - rxjava多線(xiàn)程并發(fā)怎么控制順序9. 對(duì)mysql某個(gè)字段監(jiān)控的功能10. html - vue項(xiàng)目中用到了elementUI問(wèn)題
