代码之家  ›  专栏  ›  技术社区  ›  Rosamunda

即使顺序正确,函数也没有定义?

  •  0
  • Rosamunda  · 技术社区  · 6 年前

    我用的是 lazyload

    我在我的房间里叫它 文件如下:

    lazyload();
    

    现在,在那一页的页脚我有:

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="code.jquery.com/ui/1.12.1/jquery-ui.min.js" ></script>
    <script src="/includes/jquery.lazyload.min.js"></script>
    <script src="/includes/liga.js"></script>
    

    我也尝试过这样调用函数:

    $(document).ready(function(){
    lazyload();
    ...
    

    我不断得到错误:

    ReferenceError:未定义lazyload

    为什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Martin Zeitler    6 年前

    jQuery包装器的语法是 $(selector).lazy()

    $(function() {
    
        $('.iam').lazy({
    
          /* called before an element gets handled */
          beforeLoad: function(element) {
            var imageSrc = element.data('src');
            console.log('image "' + imageSrc + '" is about to be loaded');
          },
               
          /* called after an element was successfully handled */
          afterLoad: function(element) {
            var imageSrc = element.data('src');
            console.log('image "' + imageSrc + '" was loaded successfully');
          },
                
          /* called whenever an element could not be handled */
          onError: function(element) {
            var imageSrc = element.data('src');
            console.log('image "' + imageSrc + '" could not be loaded');
          },
                
          /* called once all elements were handled */
          onFinishedAll: function() {
            console.log('finished loading all images');
          }
        });
    });
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.10/jquery.lazy.min.js"></script>
    
    <img class="iam" data-src="http://www.ektoplazm.com/img/escape-into-the-mirror.jpg" width="640" height="640" />