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

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

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

最近接觸了一個項目,要實現(xiàn)一個輪播圖的功能,因為是在原有的項目上進(jìn)行二次開發(fā),項目前端用的是layui框架,樓主是后臺方向,沒怎么接觸過前端,在用layui實現(xiàn)輪播圖時,發(fā)現(xiàn)異步從后臺獲取數(shù)據(jù),但是輪播圖片不顯示,顯示如下:

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

先貼上HTML代碼:

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

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

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

解決問題的代碼示例:

layui.use('carousel', function(){
 var carousel = layui.carousel;
 //建造實例
 var ins = carousel.render({
  elem: '#test1'
  ,width: '100%' //設(shè)置容器寬度
  ,arrow: 'always' //始終顯示箭頭
  //,anim: 'updown' //切換動畫方式
 });
 
 //這里是用jQuery異步獲取數(shù)據(jù)的大致代碼
 $.get("請求的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);

因為<div carousel-item="">加idname會報錯,所以沒用下面這種方式填充html

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

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

相關(guān)文章

最新評論