jQuery.Deferred异常:$(...)。magnificPopup不是函数

问题描述 投票:-1回答:1

我有以下标记和脚本顺序:

<div class="js-gallery">
  <div class="row gtr-50 gtr-uniform">
    <div class="col-3">
      <span class="fit image"><a href="/images/lion.jpg" title="lion">
            <img alt="lion" src="/images/lion.jpg"></a>
          </span>
    </div>
  </div>
</div>

<script src="/js/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
<script>
  $(function() {
    $('.js-gallery').magnificPopup({
      delegate: 'a',
      type: 'image',
      tLoading: 'Loading image #%curr%...',
      mainClass: 'mfp-img-mobile',
      gallery: {
        enabled: true,
        navigateByImgClick: true,
        preload: [0, 1] // Will preload 0 - before current, and 1 after the current image                      
      },
      image: {
        tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
        titleSrc: function(item) {
          return item.el.attr('title');
        }
      }
    });
  });
</script>

但是我收到以下错误:

jQuery.Deferred异常:$(...)。magnificPopup不是函数TypeError:$(...)。magnificPopup不是函数

我已检查网络选项卡并加载了大量弹出窗口

jquery magnific-popup
1个回答
0
投票

如果单独使用,您的示例将完美运行,请查看此处的代码段...

您的问题在其他地方,通常当页面加载许多jQuery实例导致冲突时会发生jQuery.Deferred异常。

<div class="js-gallery">
   <div class="row gtr-50 gtr-uniform">
       <div class="col-3">
           <span class="fit image"><a href="https://lorempixel.com/400/200/sports/1/" title="lion">
                   <img alt="lion" src="https://lorempixel.com/400/200/sports/1/"></a>
            </span>
       </div>
   </div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
<script>
  $(function () {    


     $('.js-gallery').magnificPopup({                     
         delegate: 'a',
         type: 'image',
         tLoading: 'Loading image #%curr%...',
         mainClass: 'mfp-img-mobile', 
         gallery: { 
                 enabled: true, 
                 navigateByImgClick: true,
                 preload: [0,1] // Will preload 0 - before current, and 1 after the current image                      
         },                     
         image: {                        
                tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                titleSrc: function(item) {
                         return item.el.attr('title') ; 
                }                      
          }          
     }); 

    });  
         
</script>
© www.soinside.com 2019 - 2024. All rights reserved.