簡體   English   中英

幾秒鍾后如何使用jQuery mobile隱藏div?

[英]how to hide a div after a few seconds using jQuery mobile?

網頁加載后,我想顯示5秒鍾的div,然后使其消失。 這是我的代碼:

<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

<script>
setTimeout(function() {
    $('#mask').hide();
}, 5000);
</script>
</head>
<body>

<div id="mask">
<h2>This div will fade out </h2>
</div>

</body>
</html>

上面的代碼不起作用,但是,如果我刪除了調用jQuery mobile javascript的一行,

<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

它完美地工作。 關於<head>部分中同時具有jQuery和jQuery mobile的我如何使其工作的任何想法?

jQuery初始化時,許多事件偵聽器會將您的代碼包裝在事件偵聽器上,例如:

    jQuery(document).ready(function ($) {
    setTimeout(function () {
        $('#hide').hide();
    }, 5000);
});

http://jsfiddle.net/wcuxS/1457/

嘗試在您的第一頁中包含div:

<div data-role="page" id="page1">
    <div data-role="header">
         <h1>My page</h1> 
    </div>
    <div role="main" class="ui-content">
        <div id="mask">           
            <h2>This div will fade out </h2>
        </div>
    </div>
</div>

然后在pagecreate事件中添加腳本:

$(document).on("pagecreate", "#page1", function () {
    setTimeout(function () {
        $('#mask').hide(800);
    }, 5000);
});

工作演示

您需要使用Jquery Mobile API。 將超時功能嵌入到$(document).on('pageinit')事件回調中。 另外我不確定它是否名為'pageinit',但是有一個Jquery Mobile事件,其行為類似於$(document).on('ready')。 對JQM API進行一些研究

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM