這篇文章是使用 jQuery Mobile 與 HTML5 開發 Web App 系列的第二篇,在本文以及接下來的數篇文章 Kayo 將會介紹 jQuery Mobile 的組件、事件響應以及能夠調用的方法,而做爲該系列的第一篇文章,Kayo 將會先介紹 jQuery Mobile 的基本狀況和一些基礎的實例。javascript
在上一篇文章中, Kayo 簡單介紹了漸進加強設計的概念,能夠參考文中的第四點內容。而 jQuery Mobile 雖然是一些新 web 技術( HTML五、CSS3 和 JavaScript )的產物,但對於不能提供以上技術支持的設備也會盡可能提供最好的體驗。css
根據維基百科( Wikipedia ) 的解釋,漸進加強的設計主要包括如下幾點html
由於 jQuery Mobile 使用了漸進加強的設計理念,於是它所支持的系統與平臺也很普遍,能提供 A 級支持(支持所有的加強的體驗,包括基於 Ajax 的動畫頁面轉場)的有如下平臺:html5
Apple iOS 3.2-5.0java
Android 2.1-2.3 , 3.1, 4.0jquery
Windows Phone 7-7.5web
Blackberry 6.0 , 7瀏覽器
Blackberry Playbook 1.0-2.0app
Palm WebOS 1.4-2.0 , 3.0ide
Firebox Mobile (10 Beta)
Skyfire 4.1
Opera Mobile 11.5
Meego 1.2
Samsung bada 2.0
Kindle 3 and Fire
Nook Color 1.4.1
Chrome Desktop 11-17
Firefox Desktop 4-9
Internet Explorer 7-9
Opera Desktop 10-11
注:若在實際的開發中使用到 Web SQL Database 等 HTML5 技術,則最終的 Web App 被支持度會比以上 jQuery Mobile 的被支持度低,但兩個主流的移動瀏覽器 Android 與 Apple iOS 的系統瀏覽器及其桌面版本確定能提供最好的支持。
jQuery Mobile 依賴 HTML5 data-* 屬性 來提供一系列的支持( UI 組件、過渡和頁面結構),不支持該 HTML5 屬性的瀏覽器會默認忽略這些屬性的效果,好比在頁面中添加一個版頭,能夠使用如下的 HTML:
1
2
3
|
<
div
data-role
=
"header"
>
<
h1
>jQuery Mobile Demo</
h1
>
</
div
>
|
這樣就能產生一個 jQuery Mobile 樣式的版頭,從下文的圖中能夠看出,這樣的版頭樣式很適合移動設備使用,而且在添加 data-role="header" 屬性後,div 內的 h1 也會被渲染成必定樣式,這就是 jQuery Mobile 的方便快捷,也是它的設計宗旨—— Write Less, Do More 。
除此以外 jQuery Mobile 中還有如下的 data-role 屬性(部分屬性),已經賦予了必定的樣式及用戶操做響應效果。
data-role="content" , data-role="button" , data-theme ="" , data-role="controlgroup" , data-inline="true" , data-role="fieldcontain" , data-role="listview" , data-rel="dialog" , data-transition="pop" ,分別對應着主體內容、按鈕,主題顏色,已編輯按鈕,內聯按鈕,表單元素,列表視圖,對話框,頁面過渡。
使用 jQuery Mobile ,須要在網頁頁眉中引入 jQuery Mobile 組件,包括如下部分
jQuery 庫
jQuery Mobile CSS
jQuery Mobile 庫
能夠經過這樣的 head 引入以上組件
1
2
3
4
5
6
7
8
|
<
head
>
<
title
>jQuery Mobile Demo</
title
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
/>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
>
<
link
rel
=
"stylesheet"
href
=
"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css"
/>
<
script
type
=
"text/javascript"
src
=
"http://code.jquery.com/jquery-1.6.4.min.js"
></
script
>
<
script
type
=
"text/javascript"
src
=
"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"
></
script
>
</
head
>
|
在 Android 的瀏覽器中,若沒有設定頁面寬度,它會認爲頁面寬度是 980px ,所以咱們能夠在 head 里加入一個 viewport,讓移動瀏覽器知道屏幕大小,只是一個 viewport 標籤,就已經給用戶帶來更好的體驗。
1
|
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1, maximum-scale=1.5"
>
|
在引入 jQuery Mobile 須要的組件後,咱們能夠建立 jQuery Mobile 頁面,下面給出一個簡單的例子。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>jQuery Mobile Demo</
title
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
/>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
>
<
link
rel
=
"stylesheet"
href
=
"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css"
/>
<
script
type
=
"text/javascript"
src
=
"http://code.jquery.com/jquery-1.6.4.min.js"
></
script
>
<
script
type
=
"text/javascript"
src
=
"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"
></
script
>
</
head
>
<
body
>
<
div
data-role
=
"page"
id
=
"home"
>
<
div
data-role
=
"header"
>
<
h1
>jQuery Mobile Demo</
h1
>
</
div
>
<
div
data-role
=
"content"
>
<
p
>主體內容</
p
>
</
div
>
<
div
data-role
=
"footer"
>
<
h2
>Footer</
h2
>
</
div
>
</
div
>
</
body
>
</
html
>
|
對於 jQuery Mobile ,每定義一個 data-role="page" 就至關於一個頁面, jQuery Mobile 默認採用 Ajax 的方式操做 DOM,自動隱藏除第一個頁面外的全部頁面,當點擊連接,連接到其餘頁面時會以 Ajax 的方式加載新頁面的內容,下面給出完整實例。另外,咱們還能夠使用一些 HTML5 的語義化標籤,如 header 的 div 能夠直接使用 header 標籤,具體的能夠參見下面的例子。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>jQuery Mobile Demo</
title
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
/>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
>
<
link
rel
=
"stylesheet"
href
=
"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css"
/>
<
script
type
=
"text/javascript"
src
=
"http://code.jquery.com/jquery-1.6.4.min.js"
></
script
>
<
script
type
=
"text/javascript"
src
=
"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"
></
script
>
</
head
>
<
body
>
<
div
data-role
=
"page"
id
=
"home"
>
<
header
data-role
=
"header"
>
<
h1
>jQuery Mobile Demo</
h1
>
</
header
>
<
div
data-role
=
"content"
>
<
a
href
=
"#page2"
data-role
=
"button"
>列表頁面</
a
>
</
div
>
<
footer
data-role
=
"footer"
>
<
h2
>Footer</
h2
>
</
footer
>
</
div
>
<
div
data-role
=
"page"
id
=
"page2"
>
<
header
data-role
=
"header"
>
<
h1
>jQuery Mobile Demo</
h1
>
</
header
>
<
div
data-role
=
"content"
>
<
ul
data-role
=
"listview"
data-inset
=
"true"
>
<
li
><
a
href
=
"#home"
>回到首頁</
a
></
li
>
<
li
><
a
href
=
"#home"
>回到首頁</
a
></
li
>
<
li
><
a
href
=
"#home"
>回到首頁</
a
></
li
>
</
ul
>
</
div
>
<
footer
data-role
=
"footer"
>
<
h2
>Footer</
h2
>
</
footer
>
</
div
>
</
body
>
</
html
>
|
完整實例 Demo(建議使用 PC 上的 Firefox、Chrome 等現代瀏覽器和 IE9+ 或 Android , iPhone/iPad 的系統瀏覽器瀏覽)
本文由 Kayo Lee 發表,本文連接:http://kayosite.com/web-app-by-jquery-mobile-and-html5-foundation.html