我可以将操作绑定到特定的 Facebox 吗?

发布于 2024-12-10 18:44:08 字数 795 浏览 3 评论 0原文

我想将一个函数绑定到特定facebox的关闭上?根据 facebox(at github) 上的有限文档,您可以执行类似的操作来绑定操作。

$(document).bind('close.facebox', function() {
    //do stuff...
})

但这样做的问题是,它会将相同的操作绑定到页面上创建的所有 Facebox。

那么如何将不同的功能绑定到不同的facebox呢?我尝试了一些变体,但没有成功(可能是因为我还没有掌握 javascript 和 jQuery 的技能):

  • 按照文档的建议绑定一个通用函数,并在函数中找出关闭的 Facebox 并执行所需的操作。
  • 创建 Facebox 时获取句柄并使用它来绑定操作:

    var fb = $.facebox('foo');
    fb.bind('close.facebox', function() { ...do stuff ...})
    
  • 直接在 Facebox 上调用绑定,如下所示:

    $.facebox('foo').bind('close.facebox', function() { ...do stuff ...})
    
  • 可能有更多我不记得的变体...

请帮助我理解如何我应该这样做(或者为什么我不应该尝试这样做)。

I want to bind a function to the closing of a specific facebox? According to the limited documentation at facebox(at github) you could do something like this to bind actions.

$(document).bind('close.facebox', function() {
    //do stuff...
})

But the problem with this is that it will bind the same action to all faceboxes created on the page.

So how could I bind different functions to different faceboxes? I have tried a few variants without success (probably due to my not yet masterlevelskills of javascript and jQuery):

  • Bind one general function as proposed by the documentation and in the function figure out what facebox was closed and do the wanted thing.
  • Get handle to facebox when created and use that to bind the action:

    var fb = $.facebox('foo');
    fb.bind('close.facebox', function() { ...do stuff ...})
    
  • Call bind on the facebox directly like:

    $.facebox('foo').bind('close.facebox', function() { ...do stuff ...})
    
  • Probably more variants that I do not remember...

Please help me understand how I should do this (or why I should not try to do this).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-12-17 18:44:08

快速查看源代码后,我可以想到一个 hack (!) 来实现此功能:

  1. 用您自己的方法覆盖 facebox#close 方法。
  2. 此方法中的范围将允许您访问刚刚单击的“关闭”链接
  3. 现在您可以“横向”遍历您的内容并使用例如数据属性或类名称来标识您当前显示的框
  4. 基于然后您就可以决定要做什么。

这是一个示例:

HTML:

<a href="#foo" class="facebox">Foo</a>
<a href="#bar" class="facebox">Bar</a>
<div id="foo">
    <div data-close="foo">Foo</div>
</div>
<div id="bar">
    <div data-close="bar">Bar</div>
</div>

JS:

$.facebox.close = function(e) {
    var type = $(this).prev().find('div:first').data("close");
    switch (type) {
    case "foo":
        alert("FOO");
        break;
    case "bar":
        alert("BAR");
        break;
    }
    $(document).trigger('close.facebox');
    return false
};

$('a.facebox').facebox();

在这里尝试一下:http://jsfiddle.net/SU3se/

我相信该插件假设您不会在多个“不同”对象上使用它,并且由于您有时只能打开一个 Facebox,因此这“应该”不是问题。但我可以理解为什么有人可能想这样做。

请记住,我的解决方案是一个 hack,而且真的不是很漂亮。也许其他人可以想出更好的办法。

After a quick look at the source I can think of a hack (!) to make this work:

  1. Override the facebox#close method with you own.
  2. The scope within this method will give you access to the "close" link which has just been clicked
  3. Now you can traverse "sideways" to your content and use e.g. a data attribute or class name to identify which box you're currently showing
  4. Based on that you can then make a decision what to do.

Here's an example:

HTML:

<a href="#foo" class="facebox">Foo</a>
<a href="#bar" class="facebox">Bar</a>
<div id="foo">
    <div data-close="foo">Foo</div>
</div>
<div id="bar">
    <div data-close="bar">Bar</div>
</div>

JS:

$.facebox.close = function(e) {
    var type = $(this).prev().find('div:first').data("close");
    switch (type) {
    case "foo":
        alert("FOO");
        break;
    case "bar":
        alert("BAR");
        break;
    }
    $(document).trigger('close.facebox');
    return false
};

$('a.facebox').facebox();

Try it here: http://jsfiddle.net/SU3se/ .

I believe that the plugin makes the assumption that you'll not use it on multiple "different" objects and since you can have only one facebox open at times, this "should" not be an issue. But I can see why someone might want to do it nevertheless.

Please remember that my solution is a hack and really not very pretty. Maybe someone else can think of something better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文