2017-08-03 43 views
-1

我正在尝试使用以下代码尝试从设置的目录中获取图像,并将其预加载。我不想将图像加载到一个div,而是使用普通预加载到存储器:如何使用jquery从目录加载图像

new Image().src = "/wp-content/themes/NCHERM_Theme/images/home4-events.jpg"; 

我是相当新的jQuery的所以寻找一些帮助来完成这一点:

window.onload = function($) { 
    var folder = "images/preload"; 

    $.ajax({ 
     url : folder, 
     success: function (data) { 
      //go through each item in folder 
      $.each(data, function(i,filename) { 
       //take each item and run into the normal preload method 
       setTimeout(function() { 
        // preload images 
        new Image().src = filename; 
       }, 1000); 
      }); 
     } 
    }); 
}; 

运行这个在WordPress我得到`403上的URL forbbiden”文件夹之前,我甚至可以测试,如果这是工作

+0

好像你重写了$对象。将它从参数 –

+0

代替'$'删除它尝试jQuery,对象,$,ajax到jQuery.ajax({ – LogicBlower

+0

修复,帮助解决即时错误,但现在得到403禁止在文件夹url – HollerTrain

回答

-1

确保您已将<script>标签内的jQuery参考。缺少JQuery库。

<script src="../../jquery.min.js" type="text/javascript"></script>

供您参考:$.ajax is not a function你为什么歌厅403错误,也请通过这个链接

+0

是jQuery是在WordPress内动态添加的 – HollerTrain

+0

@HollerTrain现在工作正常吗? –

+0

它被加载了整个时间,所以这里不是问题 – HollerTrain

-1

试试这个代码,如果它为你工作。

window.onload = function($) { 
var folder = "images/preload"; 

$.ajax({ 
    url : folder, 
    success: function (data) { 
     //go through each item in folder 
     $.each(data, function(i,filename) { 
      //take each item and run into the normal preload method 
      setTimeout(function() { 
       // preload images 
       var $img=$("<img />",{"src":filename}); 
       $('#target_id').append($img); 
      }, 1000); 
     }); 
    } 
}); 
}; 
+0

我不同意当你试图缩放这个脚本时,你会将数千个图像附加到一个div中,这对我没有任何意义。我不认为追加到'display:none' div是正确的方法 – HollerTrain

0

您可以通过添加解决403错误:

选项+指标

在.htaccess文件。