Javascript 如何使弹出窗口始终在顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5576596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make popup windows always on top?
提问by sams5817
I want to have a javascript/jQuery popup window (child page) that is always in front of the parent page, something like facebook's current picture viewing feature.
我想要一个始终位于父页面前面的 javascript/jQuery 弹出窗口(子页面),类似于 facebook 当前的图片查看功能。
it would be closed by the user clicking on the close button.
它会被用户点击关闭按钮关闭。
I have tried as below:
我试过如下:
mywindow = window.open ("DownloadForm.aspx", "mywindow","location=1,status=1,scrollbars=1, width=350,height=150");
mywindow.moveTo(350, 350);
this code successfully opened a child page in front of the parent page, but there is jQuery code ( $(#test).click() ) on the parent page which causes the parent page to always be in front.
这段代码成功地在父页面前面打开了一个子页面,但是父页面上有 jQuery 代码( $(#test).click() )导致父页面总是在前面。
I tried putting the window.open()
code after $(#test).click()
, but it didn't solve the problem.
我尝试将window.open()
代码放在之后$(#test).click()
,但并没有解决问题。
The $(#test).click()
is necessary, therefore I need to have a workaround.
这$(#test).click()
是必要的,因此我需要一个解决方法。
I appreciate any help, thank you in advance.
我感谢任何帮助,在此先感谢您。
回答by Mark Kahn
myWindow.focus()
Many modern browsers prevent this from working, but it's the only way.
许多现代浏览器都阻止了它的工作,但这是唯一的方法。
回答by maryam
use following javascript code in the page you are opening in popup
在弹出窗口中打开的页面中使用以下 javascript 代码
<body onblur="self.focus();">
回答by Cmail Ultimate
Just set it from the modal css. The z-index
is normally set to 1. Just change it to 9999. That is the purpose of the z-index
, to set the popup top level.
只需从模态 css 中设置它。在z-index
通常设置为1,只需将其更改为9999。也就是说的目的z-index
,设置弹出的最高水平。
If not using bootstrap popup, then you need to use css to set it. Browsers are not guaranteed to place nice with .focus()
on a window.
如果不使用 bootstrap 弹出窗口,那么你需要使用 css 来设置它。浏览器不能保证很好地放置.focus()
在窗口上。
回答by Craig White
Im sure you cannot control the order of the window. Instead of a popup (which is often and usually blocked by browsers) why not investigate using a lightbox javascript plugin. They are usually free, stylish and work well.
我确定您无法控制窗口的顺序。为什么不使用灯箱 javascript 插件进行调查,而不是弹出窗口(通常并且通常被浏览器阻止)。它们通常免费、时尚且运行良好。