类型错误:$(...).magnificPopup 不是函数

Hit*_*wat 6 html javascript magnific-popup

我使用的是 chrome 开发者版。该库未加载,我不明白为什么。chrome 控制台中报告了该错误。这是我的代码:

<ul><li><a class="mediaimage-link" href="~/img/HP/mediakit/The-CorPath-Vascular-Robotic-System_HR.jpg"><img src="~/img/HP/The-CorPath-Vascular Robotic-System-187.jpg" alt="">
</a></li>
<li><a href="https://www.youtube.com/watch?v=TnQsToiKKgw?rel=0&amp;showinfo=0" class="popup-youtube"> <img src="~/img/HP/Iyer-Video-187.jpg" alt=""></a>
         </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

$(document).ready(function() {
  //Media Image Popup
  $('.mediaimage-link').magnificPopup({type:'image'});
  //Video Iframe Popup
    $('.popup-youtube').magnificPopup({
          disableOn: 700,
          type: 'iframe',
          mainClass: 'mfp-fade',
          removalDelay: 160,
          preloader: false,
          fixedContentPos: false
        });
});`
Run Code Online (Sandbox Code Playgroud)

小智 5

您的代码中可能没有包含 JQuery 库。请记住,JQuery 文件必须放置在所有其他 Javascript 代码之上。


小智 5

$(document).ready(function ($) {
    $('.test-popup-link').magnificPopup({
    type: 'image',
    gallery: {
            enabled: true,
            navigateByImgClick: true,
            preload: [0,1] // Will preload 0 - before current, and 1 after the current image
          }
    // other options
  });
  });
Run Code Online (Sandbox Code Playgroud)

只需传入 $$(document).ready(function ($) 即可开始工作

  • 你能向我解释一下为什么这有效吗?我以前从未需要将“$”传递给`$(function() { })`。老实说,我很恼火,由于某种原因,这个库以前不需要这样做。 (2认同)