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条)

报告相同问题?

悬赏问题

  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用