doudong2149 2013-08-05 12:32
浏览 32
已采纳

使用.on()加载超薄盒子

I am using slimbox (http://www.digitalia.be/software/slimbox) a very popular lightweight lightbox for jQUery.

I have used it alot in the past, however now I am a little bit stuck. I had a form that is loading via AJAX, and therefore I need to use the .on() function to activate slimbox.

But I cannot get it work. I looked at the slimbox code and it doesnt make alot of sense to me. I tried this in my :

$("#content.settings").on("click","div.scroll a.slimbox", function (e) {
        e.preventDefault();
        $(this).slimbox();
    });

And that works, but only on the second click, for some reason it takes two clicks on the same object to function. Any ideas?

  • 写回答

2条回答 默认 最新

  • druhoytza979667566 2013-08-05 12:35
    关注

    The problem is when the element is clicked first time slimbox is not yet initialized on the element, when you click first time it gets initialized then subsequent clicks works fine.

    One possible solution is to trigger the click event once the widget is initialized

    $("#content.settings").on("click","div.scroll a.slimbox", function (e) {
        var $this = $(this);
        e.preventDefault();
        if(!$this.data('slimboxed')){
            $this.slimbox().data('slimboxed', true).click();
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条