ThickBox关闭时,您将如何触发事件



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

这有点复杂,因为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/

您可以尝试这样的方法。。。

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被触发两次,所以这包括一点黑客操作,以确保你的代码不会第二次运行。

不确定这是否是一个全局解决方案,但对于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

I认为您可以通过将点击处理程序绑定到关闭按钮来破解它:

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

如果你有选择的话,去掉thickbox(因为它不再被维护),使用一个更活跃的社区。事实上,thickbox网站提出了一些替代方案镜像

您可以将侦听器绑定到"tb_unload",当thickbox关闭时,它会触发。使用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!');
    });
}

在Thickbox 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;
}

所以当我使用自定义remove的函数时。我会这样用的。

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

我想在打开厚框时触发一个事件,我可以这样做:(希望它能帮助别人)

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

});