Close jQuery thickbox on button click

£可爱£侵袭症+ 提交于 2020-01-14 22:55:18

问题


Here is my jQuery:

<script src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/thickbox.js"></script>
<script type="text/javascript"

$('#button').click(function(){
     $('#TB_window').fadeOut();
});

</script>

Html:

  <input type="submit" id="button" value="clse" >

I tried this function, but it's not working.


回答1:


Thickbox have their built in method. tb_remove just call this method tb_remove(); wherever you need. It will close thickbox properly.

If you use $('#TB_window').fadeOut(); , TB_overlay DIV will remain open. If you again do something nonsense like $('#TB_overlay').fadeOut(); then it will create problem for your thickbox. And thickbox will stop function.




回答2:


You have to prevent the execution of the default event of the input. Change your code to this:

$('#button').click(function(){
     $('#TB_window').fadeOut();
     e.preventDefault();
});



回答3:


This solution worked for me, try it:

$(document).ready(function{
  $('#button').click(function(){
   self.parent.tb_remove();
  });
});



回答4:


If you are including thickbox.js, which is obvious, than try.....

$('#closeTBWindow').click(tb_remove);



回答5:


I suggest using "button" instead of "submit", like:

<input type="button" id="button" value="close" />

Then the jQuery code should be:

$(document).ready(function{
  $('#button').click(function(){
   $('#TB_window').fadeOut();
  });
});

Like this, you don't need to use e.preventDefault().
The $(document).ready() part is really inportant. Without this jQuery would try to attach the code to the button before it even exists, thus won't work.

side-note: I like to use DIV elements as buttons.



来源:https://stackoverflow.com/questions/11357321/close-jquery-thickbox-on-button-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!