How do I make fancybox href dynamic?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 12:46:26

问题


I have the following fancybox code:

$(\'.fancybox\').fancybox({
             \'autoScale\' : false,
             \'href\' : $(\'.fancybox\').attr(\'id\'),
             \'type\':\'iframe\',
             \'padding\' : 0,
             \'closeClick\'  : false,

//some other callbacks etc

the problem is I have twenty different A tag id\'s on the page and I want the fancybox href attribute to take the id of the clicked element, ie the one that triggered the event.

I have tried several things, none of them have worked!

\'href\' : $(this).attr(\'id\'),
\'href\' : $(this.element).attr(\'id\'),

This seems so simple but anytime I plug in \'this\' or similar nothing works.


回答1:


Without each() or click() simply add the beforeLoad callback to your script like this

$(".fancybox").fancybox({
    autoScale: false,
    // href : $('.fancybox').attr('id'), // don't need this
    type: 'iframe',
    padding: 0,
    closeClick: false,
    // other options
    beforeLoad: function () {
        var url = $(this.element).attr("id");
        this.href = url
    }
}); // fancybox

NOTE: this is for fancybox v2.0.6+

On the other hand, a more elegant solution is to use (HTML5) data-* attribute to set the href (it would look weird to set id="images/01.jpg" otherwise) so you could do :

<a href="#" id="id01" data-href="images/01.jpg" ...

and your callback

beforeLoad: function(){
 var url= $(this.element).data("href");
 this.href = url
}

and use the id attribute for what is meant for.


EDIT : The best is to use the special data-fancybox-href attribute in your anchor like :

<a id="item01" data-fancybox-href="http://jsfiddle.net" class="fancybox" rel="gallery"  href="javascript:;">jsfiddle</a>

and use a simple script without callback like

$(".fancybox").fancybox({
    // API options 
    autoScale: false,
    type: 'iframe',
    padding: 0,
    closeClick: false
});

See JSFIDDLE




回答2:


You can iterate over your collection of .fancybox items and grab the id.

$('.fancybox').each(function(){
    $(this).fancybox({
             'autoScale' : false,
             'href' : $(this).attr('id'),
             'type':'iframe',
             'padding' : 0,
             'closeClick'  : false,
             //some other callbacks etc
    });
});



回答3:


Try this:

$(".fancybox").click(function(){
    var url = $(this).attr('id');
    $.fancybox({
         'autoScale' : false,
         'href' : url ,
         'type':'iframe',
         'padding' : 0,
         'closeClick'  : false,
         //some other callbacks etc
    });
})



回答4:


Try this

$('.fancybox').each( function() {
    var elem = jQuery(this);
    elem.fancybox({
             'autoScale' : false,
             'href' : elem.attr('id'),
             'type':'iframe',
             'padding' : 0,
             'closeClick'  : false,
          });
    }
);



回答5:


Have you tried this?

$('.fancybox').each(function(){
    $(this).fancybox({
         'autoScale' : false,
         'href' : this.id,
         'type':'iframe',
         'padding' : 0,
         'closeClick'  : false,
          //some other callbacks etc
    });
});


来源:https://stackoverflow.com/questions/11618221/how-do-i-make-fancybox-href-dynamic

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