how to use jquery .html() for fancybox content

柔情痞子 提交于 2019-12-11 17:38:14

问题


my file:

<script type="text/javascript" src="{{ STATIC_URL }}js/jquery.fancybox-1.3.4.pack.js"></script>

<link rel="stylesheet" href="{{ STATIC_URL }}css/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />

{% include "submission-form.html" with section="photos" %}
<div class="commentables">
    {% load thumbnail %}

    {% for story in objects %}
        <div class="image {% if forloop.counter|add:"-1"|divisibleby:picsinrow %}left{% else %}{% if forloop.counter|divisibleby:picsinrow %}right{% else %}middle{% endif %}{% endif %}">
            {% if story.image %}
                {% thumbnail story.image size crop="center" as full_im %}
                    <a rel="gallery" href="{% url post slug=story.slug %}">
                        <img class="preview" {% if story.title %} alt="{{ story.title }}" {% endif %} src="{{ full_im.url }}">
                    </a>
                {% endthumbnail %}
            {% else %}
                {% if story.image_url %}
                    {% thumbnail story.image_url size crop="center" as full_im %}
                        <a rel="gallery" href="{% url post slug=story.slug %}">
                            <img class="preview" {% if story.title %} alt="{{ story.title }}" {% endif %} src="{{ full_im.url }}">
                        </a>
                    {% endthumbnail %}
                {% endif %}
            {% endif %}

        </div>

</div>

<script>
    $(document).ready(function(){
        $(".image a").click(function(){
            alert($(this).parent().html());
        });

       $(".image a").fancybox({
           title: $(this).attr("alt"),
           content: $(this).parent().html()
       });
    });
</script>

problem is, fancybox is loading the anchor's href's html, not the html I pass in for content, which alerts properly.

what's up?


回答1:


Unfortunately you can't use $(this) inside the fancybox (v1.3.x) function (it's a design issue), but inside any other jQuery method so try this

$(document).ready(function(){
 $(".image a").bind("click",function (){
  var data = $(this).parent().html();
  var dataTitle = $(this).attr("alt");
  function newTitle(){return "<span>"+dataTitle+"</span>";}
  $.fancybox(data,{
   "titlePosition": "inside",
   "titleFormat": newTitle
  }); // fancybox
  return false;
 }); // bind
}); //ready



回答2:


I think this one will work:

   $(".image a").click(function (){
       $.fancybox($(this).parent().html(),{
           title: $(this).attr("alt")
       });
   });


来源:https://stackoverflow.com/questions/10035575/how-to-use-jquery-html-for-fancybox-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!