javascript - jqery ajax問題
問題描述
我的問題是這樣的
當顯示了5條之后 點擊“繼續添加”鏈接時,點一次添加指定數量的數據,直到json文件中的數據取完為止也就是 顯示了5條之后,第一次點了,顯示6,7,8,第二次顯示9,10,11, 以此類推,有什么辦法解決下,我的代碼如下:
<!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>測試</title> <script src='http://m.piao2010.com/web/js/jquery-3.2.1.min.js'></script> <script src='http://m.piao2010.com/wenda/text.json'type='application/json'></script></head><body><p id='btn'>點擊</p><p id='json'></p><a href='javascript:void(0)' style='display: none'>繼續添加</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>名稱:’+res[’tip’]+’</span></br>’; str+=’<span>作者:’+res[’author’]+’</span></br></p>’; $(’.clicks’).css({display:’block’ }) $(’.num’).html(’已加載了’+(num+1)+’條數據....’);}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)+’條數據’);},error:function (res) { console.log(res)} })}) })</script><style> .lens{border-bottom: 2px solid red}</style></body></html>
問題解答
回答1:第一次請求,把返回的數據存到一個數組里,下次點擊的時候操作這個數組就行了,不用再次請求了
相關文章:
1. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????2. 在應用配置文件 app.php 中找不到’route_check_cache’配置項3. html按鍵開關如何提交我想需要的值到數據庫4. objective-c - 自定義導航條為類似美團的搜索欄樣式5. ios - vue-cli開發項目webstrom會在stylus樣式報錯,飆紅,請大神幫忙6. html5 - 用Egret寫的小游戲,怎么分享到微信呢?7. css - BEM 中塊(Block)有木有什么標準 何時決定一個部分提取為塊而不是其父級的元素呢(Element)?~8. css3 - 怎么感覺用 rem 開發的不多啊9. css - width設置為100%之后列表無法居中10. python - 在pyqt中做微信的機器人,要在表格中顯示微信好友的名字,卻顯示不出來,怎么解決?
