Javascript 如何以编程方式打开 jQuery Mobile 弹出窗口并在 5 秒后关闭它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30191760/
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 open a jQuery Mobile popup programmatically and close it after 5 seconds
提问by Yasser B.
I want to open a jQuery Mobile popup programmatically and then close it after some seconds, here is my code :
我想以编程方式打开一个 jQuery Mobile 弹出窗口,然后在几秒钟后关闭它,这是我的代码:
is there anything wrong, cause I'm not getting what I want to show
有什么问题吗,因为我没有得到我想要展示的东西
$( "#p" ).popup( "open" );
setTimeout( function(){ $( "#p" ).popup( "close" ); }, 5000 );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<div data-role="popup" id="p" data-position-to="window" data-transition="turn"><p>This is a completely basic popup, no options set.</p></div>
回答by ezanker
Make sure your code is within a page event handler like pagecreate so that jQM has initialized the popup widget:
确保您的代码位于 pagecreate 之类的页面事件处理程序中,以便 jQM 已初始化弹出窗口小部件:
<div data-role="page" id="page1">
<div data-role="header">
<h1>My page</h1>
</div>
<div role="main" class="ui-content">
<button id="btnpopup">Show Dynamic Popup</button>
</div>
<div data-role="popup" id="p" data-position-to="window" data-transition="turn"><p>This is a completely basic popup, no options set.</p></div>
</div>
$(document).on("pagecreate","#page1", function(){
$("#btnpopup").on("click", function(){
$("#p").popup("open");
setTimeout(function(){ $("#p").popup("close"); }, 5000);
});
});
Working DEMO
工作演示
回答by Amir
it is actually simple:
其实很简单:
$("#chatWindow").popup("open");

