欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決layui輪播圖有數(shù)據(jù)不顯示的情況

 更新時(shí)間:2019年09月16日 20:09:55   作者:roycon  
今天小編就為大家分享一篇解決layui輪播圖有數(shù)據(jù)不顯示的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

最近接觸了一個(gè)項(xiàng)目,要實(shí)現(xiàn)一個(gè)輪播圖的功能,因?yàn)槭窃谠械捻?xiàng)目上進(jìn)行二次開(kāi)發(fā),項(xiàng)目前端用的是layui框架,樓主是后臺(tái)方向,沒(méi)怎么接觸過(guò)前端,在用layui實(shí)現(xiàn)輪播圖時(shí),發(fā)現(xiàn)異步從后臺(tái)獲取數(shù)據(jù),但是輪播圖片不顯示,顯示如下:

用瀏覽器調(diào)試發(fā)現(xiàn),<div carousel-item="">下面已經(jīng)有幾個(gè)<div>了,說(shuō)明是有數(shù)據(jù)的。那怎么不顯示呢?后來(lái)發(fā)現(xiàn)是在獲取數(shù)據(jù)之前,頁(yè)面已經(jīng)初始化了,當(dāng)然不能顯示啦,這是時(shí)候需要在獲取數(shù)據(jù)填充html時(shí),回調(diào)reload(options)方法。

先貼上HTML代碼:

<div class="layui-carousel" id="test1" lay-filter="test1">
 <div carousel-item="">
  <script id="charts" type="text/html">
    這里是動(dòng)態(tài)遍歷的代碼
  </script>
 </div>
</div>

獲取數(shù)據(jù)代碼的反例示例:

layui.use('carousel', function(){
 var carousel = layui.carousel;
 //建造實(shí)例
 carousel.render({
  elem: '#test1'
  ,width: '100%' //設(shè)置容器寬度
  ,arrow: 'always' //始終顯示箭頭
  //,anim: 'updown' //切換動(dòng)畫(huà)方式
 });
 
 //這里是用jQuery異步獲取數(shù)據(jù)的大致代碼
 $.get("請(qǐng)求的URL",function (data) {
      var tpl = $("#charts").html();
      laytpl(tpl).render(data,function (html) {
        $("#test1").children('div').html(html);
      });
    });
});

解決問(wèn)題的代碼示例:

layui.use('carousel', function(){
 var carousel = layui.carousel;
 //建造實(shí)例
 var ins = carousel.render({
  elem: '#test1'
  ,width: '100%' //設(shè)置容器寬度
  ,arrow: 'always' //始終顯示箭頭
  //,anim: 'updown' //切換動(dòng)畫(huà)方式
 });
 
 //這里是用jQuery異步獲取數(shù)據(jù)的大致代碼
 $.get("請(qǐng)求的URL",function (data) {
      var tpl = $("#charts").html();
      laytpl(tpl).render(data,function (html) {
        $("#test1").children('div').html(html);
        //下面這步很重要
         ins.reload({elem: '#test1'});//重置輪播圖
      });
    });
});

至于為啥用

$("#test1").children('div').html(html);

因?yàn)?lt;div carousel-item="">加idname會(huì)報(bào)錯(cuò),所以沒(méi)用下面這種方式填充html

$("#idname").html(html);

以上這篇解決layui輪播圖有數(shù)據(jù)不顯示的情況就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論