2013-07-18 42 views
0

我想将Jquery脚本合并到我的Django应用程序中。该脚本是一个幻灯片 - 当我点击一个图像时,它淡出,一个新的淡入。我有两个问题 - 我不知道如何从我的jQuery脚本中调用相关图像。其次,我的HTML标头标签可能有错误,它没有选择正确的文件或库。我在Django和Jquery都很新,所以你的帮助是值得赞赏的。将Jquery与Django集成

这是我的HTML头......

<head> 
<title>Listings</title> 
{% load staticfiles %} 

<link rel="stylesheet" type="text/css" href="{% static 'noticeboard/main.css' %}"/> 

<script type="text/javascript" href="http://code.jquery.com/jquery-1.8.0.min.js"></script> 
<link rel="javascript" type="text/javascript" href="{% static 'noticeboard/main.js' %}"/> 
</head> 

下面是相关的div ...

<div id = "image-gallery"> 
    {% if listing.image1 %} 
    <img id = "picture" src ="{{ listing.image1.url }}"> 
    {% endif %} 
</div> 

这里是我的jQuery脚本。我猜var ImageName是不正确的。我需要在这里调用相关的图像文件,只是不知道如何。

$(document).ready(function() { 
    var imageName = ["imgres.jpeg", "imgres2.jpeg", "imgres3.jpeg"]; 
    var indexNum = 0; 

    $("#picture").click(function() { 

     $("#picture").fadeOut(30, function() { 
      $("#picture").attr("src", imageName[indexNum]); 
      indexNum++; 
      if (indexNum > 2) {indexNum = 0;} 
      $("#picture").fadeIn(50); 
     }); 
    }); 
}); 
+0

的static_url添加到'变种imageName = [ “imgres.jpeg”, “imgres2.jpeg”, “imgres3.jpeg”];'所以它看起来像'变种imageName = [“我的 - static-url/imgres.jpeg“,”my-static-url/imgres2.jpeg“,”my-static-url/imgres3.jpeg“];' – nickromano

回答

0
var imageName = [{% for image_obj in image_objs %}"{{image_obj.image_field.url}}"{% if not forloop.last %},{% endif %} {% endfor %}]; 
+0

感谢Sandeep,但并不是Jquery脚本必须成为HTML代码为了让我使用上面的Django模板?他们目前在单独的文件中。 –

+0

为了让jquery工作,你必须在你的html文件中包含