如何以编程方式打开jQuery Mobile弹出窗口并在5秒后关闭它

时间:2015-05-12 13:03:51

标签: javascript html jquery-mobile popup

我想以编程方式打开一个jQuery Mobile弹出窗口,然后在几秒钟后关闭它,这是我的代码:

有什么不对,因为我没有得到我想要展示的内容



$( "#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>
&#13;
&#13;
&#13;

JSFIDDLE DEMO

2 个答案:

答案 0 :(得分:13)

确保您的代码位于页面事件处理程序(如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);
    });
});
  

正在使用 DEMO

答案 1 :(得分:7)

实际上很简单:

 $("#chatWindow").popup("open");