【jQuery Mobile學習】jQuery Mobile頁面(一)

來源:互聯網
上載者:User

        jQuery Mobile大致上跟一般的HTML頁面大致一樣,不同之處就在於加了"<meta name="viewport" content="width=device-width, initial-scale=1">"這一代碼主要作用就是初始化瀏覽器顯示的寬度、縮放比例。整體如下:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Jquery mobile</title>    <!--初始化行動裝置瀏覽器顯示,針對螢幕大小不一來實現各個瀏覽器的相容-->    <!--"width=device-width"是說寬度等於螢幕寬度,"initial-scale=1"是比例1,就是預設不縮放-->    <meta name="viewport" content="width=device-width, initial-scale=1">    <!--引入jquery mobile的樣式-->    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.css"/>    <script type="text/javascript" src="js/jquery.min.js"></script></head><body><!--引入jquery--><script src="js/jquery-1.11.1.js"></script><!--引入jquery mobile指令碼--><script src="js/jquery.mobile-1.4.5.js"></script></body></html>

        之前說過jquery mobile跟jquery類似,從上面的範例程式碼就可以看出,引入樣式表跟JS指令碼,jquery mobile就可以開始工作了。jquery mobile主要是通過HTML5的data-*這個屬性來建立行動裝置的互動頁面。一般來說,一個網頁一般分為標題列、內容、頁尾三部分,當然只有內容也是可以的。

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Jquery mobile</title>    <!--初始化行動裝置瀏覽器顯示,針對螢幕大小不一來實現各個瀏覽器的相容-->    <!--"width=device-width"是說寬度等於螢幕寬度,"initial-scale=1"是比例1,就是預設不縮放-->    <meta name="viewport" content="width=device-width, initial-scale=1">    <!--引入jquery mobile的樣式-->    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.css"/>    <script type="text/javascript" src="js/jquery.min.js"></script></head><body>    <div data-role="page">       <div data-role="header"><h2>我是頭部</h2></div>       <div role="main" class="ui-content"><p>我是主體</p></div>       <div data-role="footer"><h2>我是底部</h2></div>    </div>    <!--引入jquery-->    <script src="js/jquery-1.11.1.js"></script>    <!--引入jquery mobile指令碼-->    <script src="js/jquery.mobile-1.4.5.js"></script></body></html>

運行上面這段代碼,如圖所示:

這是我在Chrome瀏覽器用手機模擬器看到的頁面效果,不需要寫樣式來控制頁面顯示效果,這裡要注意的是使用頭部和底部使用data-role="",而主體使用role="",當然主體使用data-role也是沒問題,這樣做是為了區分開是內容還是標題列。

        關於調試,我這裡推薦使用Chrome瀏覽器,也就是Google瀏覽器,因為它的相容性和調試性更好,同時我們在jquery mobile中,可以在一個html檔案中建立多個頁面,這跟我們傳統的web頁面不同,以前的我們一個html頁面要跳轉到另一個html頁面,或者需要在一個頁面跳轉到多個頁面中,我們常用iframe等等的方式進行跳轉,但是在jquery mobile中,我們可以用一個頁面就完成一個webapp(就是整個項目),說白了就是能夠在一個html檔案中建立多個頁面,哪jquery mobile是通過什麼方式來區分多個頁面和寫多個頁面呢。這時,我們只需要通過唯一的id來分隔每張頁面,並使用href屬性來連結各個頁面,具體代碼實現如下:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Jquery mobile</title>    <!--初始化行動裝置瀏覽器顯示,針對螢幕大小不一來實現各個瀏覽器的相容-->    <!--"width=device-width"是說寬度等於螢幕寬度,"initial-scale=1"是比例1,就是預設不縮放-->    <meta name="viewport" content="width=device-width, initial-scale=1">    <!--引入jquery mobile的樣式-->    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.css"/>    <script type="text/javascript" src="js/jquery.min.js"></script></head><body>    <div data-role="page" id="index">       <div data-role="header"><h2>我是頭部1</h2></div>       <div role="main" class="ui-content"><p>我是主體1</p></div>       <div data-role="footer"><h2>我是底部1</h2></div>    </div>     <div data-role="page" id="index2">       <div data-role="header"><h2>我是頭部2</h2></div>       <div role="main" class="ui-content"><p>我是主體2</p></div>       <div data-role="footer"><h2>我是底部2</h2></div>    </div>    <!--引入jquery-->    <script src="js/jquery-1.11.1.js"></script>    <!--引入jquery mobile指令碼-->    <script src="js/jquery.mobile-1.4.5.js"></script></body></html>

運行這段代碼我們會發現第二個頁面沒有顯示,所以這時我們需要設定一個連結,來進行頁面之間的跳轉,具體如下,

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Jquery mobile</title>    <!--初始化行動裝置瀏覽器顯示,針對螢幕大小不一來實現各個瀏覽器的相容-->    <!--"width=device-width"是說寬度等於螢幕寬度,"initial-scale=1"是比例1,就是預設不縮放-->    <meta name="viewport" content="width=device-width, initial-scale=1">    <!--引入jquery mobile的樣式-->    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.css"/>    <script type="text/javascript" src="js/jquery.min.js"></script></head><body>    <div data-role="page" id="index">       <div data-role="header">          <h2>我是頭部1</h2>       </div>       <div role="main" class="ui-content">          <p>我是主體1</p>          <p><a href="#index2">index2</a></p>       </div>       <div data-role="footer">          <h2>我是底部1</h2>       </div>    </div>     <div data-role="page" id="index2">       <div data-role="header">          <h2>我是頭部2</h2>       </div>       <div role="main" class="ui-content">          <p>我是主體2</p>          <p><a href="#index">index</a></p>       </div>       <div data-role="footer">          <h2>我是底部2</h2>       </div>    </div>    <!--引入jquery-->    <script src="js/jquery-1.11.1.js"></script>    <!--引入jquery mobile指令碼-->    <script src="js/jquery.mobile-1.4.5.js"></script></body></html>

運行這段代碼,點擊頁面的上的超連結就會跳轉到對應的頁面上。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.