简体   繁体   English

单击按钮关闭窗口

[英]Close a window on button click

How to close a tab or window by clicking on a button in JQuery? 如何通过单击JQuery中的按钮来关闭选项卡或窗口? I tried window.close() thinking that it would close that window,but it doesn't seem to work. 我尝试了window.close(),以为它将关闭该窗口,但似乎不起作用。 Someone help me. 谁来帮帮我。

<?php echo $javascript->link('jquery');?>
<script type="java/javascript">
    $(document).ready(function(){
            $(".close").click(function(){ 
                    window.close();
            });
    });
</script>
<div class="close">
   <?php echo $form->button('Close Window');?>
</div>

EDIT 编辑

Sorry,The code is correct. 对不起,代码正确。 The answer didn't come due to a very silly mistake of mine. 答案不是由于我的一个非常愚蠢的错误而来的。 I had typed script type as java/javascript instead of text/javascript.Now I have changed it and this works fine. 我已经将脚本类型键入为java / javascript而不是text / javascript。现在,我对其进行了更改,效果很好。

As seth comments, your problem is the handler not being attached because the browser won't execute java/javascript code as it doesn't know what to do with it (since it is nothing). 正如塞思所评论的那样,您的问题是处理程序未附加,因为浏览器不执行Java / javascript代码,因为它不知道如何处理(因为它什么也没有)。 Change it to text/javascript and you should be fine. 将其更改为text / javascript,就可以了。

There is actually a function codeBlock in JavascriptHelper that inserts a well-formed JavaScript script tag for you, including the CDATA section delimiters. 实际上, JavascriptHelper中有一个功能codeBlock ,可为您插入格式良好的JavaScript脚本标记,包括CDATA节定界符。 Used together with PHP's heredoc I think it's a quite neat solution. PHP的Heredoc一起使用,我认为这是一个非常简洁的解决方案。 At least it spares you from tiresome mistakes like these. 至少,它可以避免您犯下这些令人讨厌的错误。 :)

<a href="javascript:window.open('','_self').close();">close</a>

如果浏览器窗口未由Javascript打开,则无法由脚本关闭。

您将click事件放在div而不是按钮上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM