当 ThickBox 关闭时,如何触发事件?

发布于 2024-11-08 20:57:10 字数 105 浏览 1 评论 0原文

这个问题与 WordPress 半相关,但在其他地方也有应用。基本上,我试图做到这一点,以便当有人退出 Thickbox 时,它会触发页面其他位置的事件。编辑 Thickbox 文件不是一个选项。

This question is Semi-related to Wordpress, but has applications elsewhere. Basically, I'm trying to make it so when someone exits out of a Thickbox, it triggers an event elsewhere on the page. Editting the Thickbox file isn't an option.

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

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

发布评论

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

评论(7

画尸师 2024-11-15 20:57:10

这有点复杂,因为 Thickbox 不是这样写的。但也许你可以使用一些技巧来做到这一点。

这不是推荐的解决方案,但您可以“重写”关闭函数。类似于:

var old_tb_remove = window.tb_remove;

var tb_remove = function() {
    old_tb_remove(); // calls the tb_remove() of the Thickbox plugin
    alert('ohai');
};

工作 \o/ http://jsfiddle.net/8q2JK/1/

It's a bit complicated since Thickbox isn't written that way. But maybe you can use some tricks to do it.

It's not the recommended solution but you can "rewrite" the close function. Something like:

var old_tb_remove = window.tb_remove;

var tb_remove = function() {
    old_tb_remove(); // calls the tb_remove() of the Thickbox plugin
    alert('ohai');
};

Works \o/ http://jsfiddle.net/8q2JK/1/

沫尐诺 2024-11-15 20:57:10

您可以尝试这样的操作...

var tb_unload_count = 1;
$(window).bind('tb_unload', function () {
    if (tb_unload_count > 1) {
        tb_unload_count = 1;
    } else {
        // do something here
        tb_unload_count = tb_unload_count + 1;
    }
});

tb_unload 被触发两次,因此这包括一些黑客操作,以确保您的代码不会第二次运行。

You can try something like this...

var tb_unload_count = 1;
$(window).bind('tb_unload', function () {
    if (tb_unload_count > 1) {
        tb_unload_count = 1;
    } else {
        // do something here
        tb_unload_count = tb_unload_count + 1;
    }
});

tb_unload is triggered twice so this includes a bit of a hack to make sure your code doesn't run the second time.

无人问我粥可暖 2024-11-15 20:57:10

不确定这是否是一个全局解决方案,但对于 WordPress,至少对于最新版本(4.9.5),您可以使用:

jQuery( 'body' ).on( 'thickbox:removed', function() {
    // Your code here.
});

在 Thickbox v3.1 中,tb_remove() 调用:

jQuery( 'body' ).trigger( 'thickbox:removed' );

请参阅:https://github.com/WordPress/WordPress/blob/master/wp-includes/js/thickbox/thickbox.js#L290

Not sure if this is a global solution, but for WordPress, at least for the latest version (4.9.5), you can use:

jQuery( 'body' ).on( 'thickbox:removed', function() {
    // Your code here.
});

In Thickbox v3.1, tb_remove() calls:

jQuery( 'body' ).trigger( 'thickbox:removed' );

See: https://github.com/WordPress/WordPress/blob/master/wp-includes/js/thickbox/thickbox.js#L290

冷默言语 2024-11-15 20:57:10

我认为您可以通过将点击处理程序绑定到关闭按钮来解决这个问题:

$("#TB_closeWindowButton").click(function() {
    doSomething();
});

如果您有选择,请摆脱厚盒(因为它不再维护)并使用社区更活跃的东西。事实上,thickbox 网站提出了一些替代方案 镜像

I think you can hack that by binding a click handler to the close button:

$("#TB_closeWindowButton").click(function() {
    doSomething();
});

If you have a choice, get rid of thickbox (as it is no longer maintained) and use something with a more active community. The thickbox site, in fact, proposes some alternatives mirror.

从此见与不见 2024-11-15 20:57:10

您可以将侦听器绑定到“tb_unload”,该侦听器会在厚盒关闭时触发。使用 jQuery 的示例:

<input id="tb_button" type="button" value="Click to open thickbox" />

jQuery('#tb_button').on('click', function(){
      tb_show('This is Google in a thickbox', 'http://www.google.com?TB_iframe=true');
      jQuery('#TB_window').on("tb_unload", function(){
        alert('Triggered!');
    });
}

You can bind a listener to the "tb_unload" which triggers on when the thickbox is closed. Example using jQuery:

<input id="tb_button" type="button" value="Click to open thickbox" />

jQuery('#tb_button').on('click', function(){
      tb_show('This is Google in a thickbox', 'http://www.google.com?TB_iframe=true');
      jQuery('#TB_window').on("tb_unload", function(){
        alert('Triggered!');
    });
}
抚笙 2024-11-15 20:57:10

在厚盒 3.1 上,
我将进入thickbox-fixed.js
并会为自己添加一个自定义函数,

只是为了添加一个回调函数,不确定是不是一个好方法,但它对我的项目有用。

function mycustom_tb_remove(callback) {
  $("#TB_imageOff").unbind("click");
  $("#TB_closeWindowButton").unbind("click");
  $("#TB_window").fadeOut("fast",function(){
    if(callback)callback();
    $('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
  });
  $("#TB_load").remove();
  if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
    $("body","html").css({height: "auto", width: "auto"});
    $("html").css("overflow","");
  }
  document.onkeydown = "";
  document.onkeyup = "";
  return false;
}

所以当我使用自定义删除功能时。我会这样使用它。

self.parent.mycustom_tb_remove(function(){
    alert("Closed");
});

On Thickbox 3.1,
I will go inside thickbox-fixed.js
and will add a custom function for myself

just to add a callback function, not sure is a good way, but its work for my project.

function mycustom_tb_remove(callback) {
  $("#TB_imageOff").unbind("click");
  $("#TB_closeWindowButton").unbind("click");
  $("#TB_window").fadeOut("fast",function(){
    if(callback)callback();
    $('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
  });
  $("#TB_load").remove();
  if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
    $("body","html").css({height: "auto", width: "auto"});
    $("html").css("overflow","");
  }
  document.onkeydown = "";
  document.onkeyup = "";
  return false;
}

So when I use my custom remove's function. I will use it this way.

self.parent.mycustom_tb_remove(function(){
    alert("Closed");
});
云淡风轻 2024-11-15 20:57:10

我想在打开厚盒子时触发一个事件我可以这样做:(希望它对某人有帮助)

jQuery( 'body' ).on( 'thickbox:iframe:loaded', function ( event ) {
//Your trigger code here.

});

I wanted to trigger an event when thick box is opened I was able to do it this way: ( hope it helps someone )

jQuery( 'body' ).on( 'thickbox:iframe:loaded', function ( event ) {
//Your trigger code here.

});

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