事件 = 所有不同訪問者訪問頁面的響應(yīng)動作。
jQuery Mobile 事件
在jQuery Mobile你可以使用任何標準的 jQuery 事件 。
除此之外, jQuery Mobile 也提供了針對移動端瀏覽器的事件:
觸摸事件 - 當用戶觸摸屏幕時觸發(fā)
滑動事件 - 當用戶上下滑動時觸發(fā)
定位事件 - 當設(shè)備水平或垂直翻轉(zhuǎn)時觸發(fā)
頁面事件 - 當頁面顯示,隱藏,創(chuàng)建,加載或未加載時觸發(fā)
初始化 jQuery Mobile 事件
在學習jQuery時我們學到了用$(document).ready()來使你的jQuery代碼腳本在DOM元素加載完成后才開始執(zhí)行:
實例
<!DOCTYPE html> <html> <head> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").on("click",function(){ $(this).hide(); }); }); </script> </head> <body> <p>如果您點擊我,我會消失。</p> <p>點擊我,我會消失。</p> <p>點擊我,我也會消失。</p> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
但是,在 jQuery Mobile 中, 使用pageinit 事件來設(shè)置代碼腳本在DOM元素加載完成后開始執(zhí)行,所以要在任何新頁面加載并創(chuàng)建是執(zhí)行腳本,就需要綁定pageinit事件。
第二個參數(shù) ("#pageone")為指定事件的頁面id:
實例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("click",function(){ $(this).hide(); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>頁眉文本</h1> </div> <div data-role="main" class="ui-content"> <p>如果您點擊我,我會消失。</p> <p>點擊我,我會消失。</p> <p>點擊我,我也會消失。</p> </div> <div data-role="footer"> <h1>頁腳文本</h1> </div> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
實例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("click",function(){ $(this).hide(); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>頁眉文本</h1> </div> <div data-role="main" class="ui-content"> <p>如果您點擊我,我會消失。</p> <p>點擊我,我會消失。</p> <p>點擊我,我也會消失。</p> </div> <div data-role="footer"> <h1>頁腳文本</h1> </div> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
實例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("click",function(){ $(this).hide(); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>頁眉文本</h1> </div> <div data-role="main" class="ui-content"> <p>如果您點擊我,我會消失。</p> <p>點擊我,我會消失。</p> <p>點擊我,我也會消失。</p> </div> <div data-role="footer"> <h1>頁腳文本</h1> </div> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
注意: jQuery on() 方法用于綁定事件到選中的元素上。 |
下一章節(jié)我們將更詳細介紹 jQuery Mobile 事件。