Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何通过单击来关闭弹出窗口?_Jquery_Html_Css - Fatal编程技术网

Jquery 如何通过单击来关闭弹出窗口?

Jquery 如何通过单击来关闭弹出窗口?,jquery,html,css,Jquery,Html,Css,我创建了一个弹出窗口,打开点击图像的较大副本。这是它的html代码: <img src="images/car1_1.jpg" alt="interior" id=Upics onClick="MM_openBrWindow('images/car1_1.jpg','CARPOP','width=500em,height=500em')" /> 弹出窗口的jquery/脚本: <script type="text/javascript"> <!-- functio

我创建了一个弹出窗口,打开点击图像的较大副本。这是它的html代码:

<img src="images/car1_1.jpg" alt="interior" id=Upics onClick="MM_openBrWindow('images/car1_1.jpg','CARPOP','width=500em,height=500em')" />
弹出窗口的jquery/脚本:

<script type="text/javascript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>
单击窗口时是否有关闭窗口的方法?

Javascript的窗口。关闭关闭脚本所连接的窗口。在您的情况下,如果将此js代码添加到弹出窗口的脚本中,您可以通过单击事件关闭其窗口,如下所示:

main.html

popup.html


如果图像没有覆盖整个页面,请调整CSS。

首先,您需要为弹出窗口创建一个变量/引用,例如:

<script type="text/javascript">
<!--
var myPopup;

function MM_openBrWindow(theURL,winName,features) { //v2.0
  myPopup = window.open(theURL,winName,features);
}
//-->
</script>
解决方案1: 使用windoe.close方法

  popup_window = window.open("");

  popup_window.close ();
解决方案2:
您可以使用self.close关闭当前窗口

首先,我将弹出引用存储在一个名为imgPopup的变量中。在主体上编写了一个单击事件,以关闭弹出的imgPopup.close。在调用close函数之前,我正在检查引用是否存在。如果单击当前窗口上的任意位置,弹出窗口将关闭

<script type="text/javascript">
var imgPopup;
function MM_openBrWindow(theURL,winName,features) { //v2.0
  imgPopup = window.open(theURL,winName,features);
}
jQuery(document).ready(function() {
// Close the window when we click on the body.
    jQuery('body').click(function() { 
        if (imgPopup) {
            imgPopup.close(); 
        } 
    });
});
</script>

如果单击图像上除此之外的任何位置,模式窗口将关闭

您在其他位置的意思是什么?您可以将该语句传递给事件处理程序或其他什么东西。如何关闭当前窗口?您不应该只粘贴代码,而不在其旁边写任何类似的内容。@AdrienBrunelat-我在注释中说明了我正在做的事情。当我们点击当前窗口当前窗口的主体时,我正在关闭新窗口。如果这还不够的话,我可以写得更清楚。是的,请事先加一点文字来解释你要展示的内容。特别是,代码的哪一部分使其不同,或者OP的代码与您的代码之间有什么区别,使其更易于阅读。@AdrienBrunelat-谢谢。我添加了说明。你必须确保你的div捕获了点击事件。哦,弹出窗口的URL不是指向你写的页面的?如果没有,你应该把它添加到你原来的帖子中。我真的不明白你的意思。你打开的弹出窗口是一个HTML页面,对吗?你创建了它吗?你有权访问它的源代码吗?这不是我创建的HTML页面,当创建较小的图像时,弹出窗口上会显示较大的图片DHM,我认为OP希望在单击弹出窗口时关闭弹出窗口,而不是主窗口。如果其中一个解决方案回答了你的问题,请接受它,否则,请提供更多详细信息。
myPopup.close();
  popup_window = window.open("");

  popup_window.close ();
<script type="text/javascript">
var imgPopup;
function MM_openBrWindow(theURL,winName,features) { //v2.0
  imgPopup = window.open(theURL,winName,features);
}
jQuery(document).ready(function() {
// Close the window when we click on the body.
    jQuery('body').click(function() { 
        if (imgPopup) {
            imgPopup.close(); 
        } 
    });
});
</script>
var myWindow;
var count = 0;
function MM_openBrWindow(theURL,winName,features) {
    myWindow =   window.open(theURL,winName,features);
}

$("body").on("click",function(){
    var url  =  $("body").find('img').attr('src') || "";
    if(url != "" && count == 0){
        count = 1;
    }else{
        count = 0;
        myWindow.close();
    }

})