溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ajax請求json數(shù)據(jù)的實例講解

發(fā)布時間:2021-08-04 21:20:37 來源:億速云 閱讀:132 作者:chen 欄目:web開發(fā)

這篇文章主要介紹“ajax請求json數(shù)據(jù)的實例講解”,在日常操作中,相信很多人在ajax請求json數(shù)據(jù)的實例講解問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ajax請求json數(shù)據(jù)的實例講解”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

今天有這樣一個需求,點擊六個大洲,出現(xiàn)對應(yīng)的一些請求信息,展示在下面,請求請求過后,第二次點擊就無需請求。
如圖所示:點擊北美洲下面出現(xiàn)請求的一些數(shù)據(jù)

ajax請求json數(shù)據(jù)的實例講解

html代碼結(jié)構(gòu):

<div class="conSixmap">
  <div class="name conmap01" data-name="beimeizhou">
    <a href="javascript:void(0)">北美洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap02" data-name="nanmeizhou">
    <a href="javascript:void(0)">南美洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap03" data-name="ouzhou">
    <a href="javascript:void(0)">歐洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap04" data-name="feizhou">
    <a href="javascript:void(0)">非洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap05" data-name="yazhou">
    <a href="javascript:void(0)">亞洲</a>
    <div class="condetail"></div>
  </div>
  <div class="name conmap06" data-name="dayangzhou">
    <a href="javascript:void(0)">大洋洲</a>
    <div class="condetail"></div>
  </div>
</div>

css樣式:

.conSixmap{position:relative;width:678px;height:335px;margin:0 auto;background:url(../images/tuanduimapBg.png) no-repeat;color:#000;font-family:"微軟雅黑"}
.conSixmap .name .condetail{display:none;position:absolute;z-index:10;width:216px;padding:10px;left:50%;margin-left:-118px;top:54px;background:url(../images/opcity83.png) repeat;border-radius:5px;}
.conSixmap .condetail span{display:block;color:#fff;font-size:14px;text-align:left;}
.conSixmap .name{position:absolute;width:52px;height:55px;}
.conSixmap .name a{display:block;z-index:2;position:absolute;padding-top:35px;text-align:center;cursor:pointer;width:52px;height:20px;color:#000;font-size:12px;}
.conSixmap .conmap01{left:91px;top:73px;}
.conSixmap .conmap01 a{background:url(../images/beimeipicBg.png) no-repeat top center;}
.conSixmap .conmap02 {left:180px;top:213px;}
.conSixmap .conmap02 a{background:url(../images/nanmeimapbg.png) no-repeat top center;}
.conSixmap .conmap03 {left:339px;top:68px;}
.conSixmap .conmap03 a{background:url(../images/ouzhoumapBg.png) no-repeat top center;}
.conSixmap .conmap04{left:327px;top:158px;}
.conSixmap .conmap04 a{background:url(../images/feizhoumapbg.png) no-repeat top center;}
.conSixmap .conmap05 {left:480px;top:75px;}
.conSixmap .conmap05 a{background:url(../images/yazhoumapBg.png) no-repeat top center;}
.conSixmap .conmap06 {left:545px;top:220px;}
.conSixmap .conmap06 a{background:url(../images/dayangmapbg.png) no-repeat top center;}

json格式:

{
  "beimeizhou": [
    "請求的json數(shù)據(jù)1",
    "請求的json數(shù)據(jù)2"
  ],
  "nanmeizhou": [
    "請求的json數(shù)據(jù)3",
    "請求的json數(shù)據(jù)4"
  ],
  "ouzhou": [
    "請求的json數(shù)據(jù)5",
    "請求的json數(shù)據(jù)6",
    "請求的json數(shù)據(jù)7",
    "請求的json數(shù)據(jù)8"
  ],
  "feizhou": [
    "請求的json數(shù)據(jù)9",
    "請求的json數(shù)據(jù)10",
    "請求的json數(shù)據(jù)11",
    "請求的json數(shù)據(jù)12"
  ],
  "yazhou": [
    "請求的json數(shù)據(jù)13",
    "請求的json數(shù)據(jù)14"
  ],
  "dayangzhou": [
    "請求的json數(shù)據(jù)15",
    "請求的json數(shù)據(jù)16"
  ]
}

js代碼:

$(document).ready(function(){
  //添加地圖
  var stauteArr={
      'beimeizhou':'true',
      'nanmeizhou':'true',
      'ouzhou':'true',
      'feizhou':'true',
      'yazhou':'true',
      'dayangzhou':'true'
    };
  $(".conSixmap .name").on('click',function(){
    var _this=this;
    var htmlcon="";
    $(this).siblings(".name").children(".condetail").fadeOut(500);
    $(this).children(".condetail").fadeIn(500);
    var _name=$(this).attr('data-name');
     $.ajax({
      url:"js/schoolMap.json",
      type:'get',
      data:{},
      dataType:"json",
      success: function(data){
        for(var i in data){
          if(_name==i && stauteArr[i]=='true'){
            for(var j=0;j<data[i].length;j++){
               htmlcon+="<span>"+data[i][j]+"</span>";
            }
            $(_this).children(".condetail").append(htmlcon);
            stauteArr[i]='false';
          }
        }
      },
      error: function(){
        alert('請求失敗,請檢查網(wǎng)絡(luò)');
      }
    });
  });
});

到此,關(guān)于“ajax請求json數(shù)據(jù)的實例講解”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI