[关闭]
@mynameiszhangxu 2015-04-13T06:24:14.000000Z 字数 26151 阅读 2198

jQuery Mobile

笔记


从 CDN 引用 jQuery Mobile

与 jQuery 类似,无需在您的计算机上安装任何程序;您只需直接在 HTML 页面中引用以下样式表和 JavaScript 库,这样 jQuery Mobile 就可以工作了:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  7. </head>
  8. <body>
  9. <div data-role="page" id="pageone">
  10. <div data-role="header">
  11. <h1>欢迎访问我的主页</h1>
  12. </div>
  13. <div data-role="content">
  14. <p>欢迎!</p>
  15. </div>
  16. <div data-role="footer">
  17. <h1>页脚文本</h1>
  18. </div>
  19. </div>
  20. </body>
  21. </html>

在 jQuery Mobile 中添加页面

页面

在 jQuery Mobile,您可以在单一 HTML 文件中创建多个页面。
以最外层的<div> </div>为分隔页面的标记
请通过唯一的 id来分隔每张页面,并使用 href 属性来连接彼此:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  7. </head>
  8. <body>
  9. <div data-role="page" id="pageone">
  10. <div data-role="header">
  11. <h1>欢迎访问我的主页</h1>
  12. </div>
  13. <div data-role="content">
  14. <p>Welcome!</p>
  15. <a href="#pagetwo">转到页面二</a>
  16. </div>
  17. <div data-role="footer">
  18. <h1>页脚文本</h1>
  19. </div>
  20. </div>
  21. <div data-role="page" id="pagetwo">
  22. <div data-role="header">
  23. <h1>欢迎访问我的主页</h1>
  24. </div>
  25. <div data-role="content">
  26. <p>Goodbye!</p>
  27. <a href="#pageone">转到页面一</a>
  28. </div>
  29. <div data-role="footer">
  30. <h1>页脚文本</h1>
  31. </div>
  32. </div>
  33. </body>
  34. </html>

添加对话框

对话框是用来显示信息或请求输入的视窗类型。

如需在用户点击(轻触)链接时创建一个对话框,请向该链接添加 data-rel="dialog":

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  7. </head>
  8. <body>
  9. <div data-role="page" id="pageone">
  10. <div data-role="header">
  11. <h1>欢迎访问我的主页</h1>
  12. </div>
  13. <div data-role="content">
  14. <p>欢迎!</p>
  15. <a href="#pagetwo" data-rel="dialog">转到页面二</a>
  16. </div>
  17. <div data-role="footer"> <div data-role="header">
  18. <h1>欢迎访问我的主页</h1>
  19. </div>
  20. <h1>页脚文本</h1>
  21. </div>
  22. </div>
  23. <div data-role="page" id="pagetwo">
  24. <div data-role="header">
  25. <h1>我是一个对话框!</h1>
  26. </div>
  27. <div data-role="content">
  28. <p>对话框与普通页面不同,它显示在当前页面的顶端。它不会横跨整个页面宽度。对话框页眉中的图标 “X” 可关闭对话框。</p>
  29. <a href="#pageone">转到页面一</a>
  30. </div>
  31. <div data-role="footer">
  32. <h1>页脚文本</h1>
  33. </div>
  34. </div>
  35. </body>
  36. </html>

界面切换效果

过度 描述
fade 默认。淡入淡出到下一页
flip 从后向前翻动到下一页
flow 抛出当前页面进入下一页
pop 像弹出窗口一样进入下一页
slide 从右向左滑动到下一页
slidefade 从左向右滑并淡出到下页
slideup
slidedown
turn
none 无效果

滑动切换

  1. data-transition="slide" data-direction="reverse"

上行代码中,data-transition="slide"表示切换方式为滑动;data-direction="reverse"表示滑动方向为反向。代码效果为反向滑动切换页面

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  7. </head>
  8. <body>
  9. <div data-role="page" id="pageone">
  10. <div data-role="header">
  11. <h1>欢迎来到我的主页</h1>
  12. </div>
  13. <div data-role="content">
  14. <p>点击链接来查看滑动效果(从右向左滑动到下一页)</p>
  15. <a href="#pagetwo" data-transition="slide">滑动到页面二</a>
  16. </div>
  17. <div data-role="footer">
  18. <h1>页脚文本</h1>
  19. </div>
  20. </div>
  21. <div data-role="page" id="pagetwo">
  22. <div data-role="header">usr/bin/pyt
  23. <h1>欢迎来到我的主页</h1>
  24. </div>
  25. <div data-role="content">
  26. <p>点击链接来查看反向的滑动效果(从左向右滑动到前一页)</p>
  27. <a href="#pageone" data-transition="slide" data-direction="reverse">滑动到页面一(反向)</a>
  28. </div>
  29. <div data-role="footer">
  30. <h1>页脚文本</h1>
  31. </div>
  32. </div>
  33. </body>
  34. </html>

button按钮

在 jQuery Mobile 中创建按钮

jQuery Mobile 中的按钮可通过三种方法创建:

使用 <button> 元素
使用 <input> 元素
使用 data-role="button" 的 <a> 元素

按钮作为链接(导航)

  1. <a href="#pagetwo" data-role="button">转到页面二</a>
行内按钮(调节按钮大小)

默认情况下,按钮会占据屏幕的全部宽度。如果您需要按钮适应其内容,或者如果您需要两个或多个按钮并排显示,请添加 data-inline="true":
实例

  1. <a href="#pagetwo" data-role="button" data-inline="true">转到页面二</a>
组合按钮

jQuery Mobile 提供了对按钮进行组合的简单方法。

请将 data-role="controlgroup"属性与data-type="horizontal|vertical" 一同使用,以规定水平或垂直地组合按钮:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  7. </head>
  8. <body>
  9. <div data-role="page" id="pageone">
  10. <div data-role="header">
  11. <h1>分组按钮</h1>亲自试
  12. </div>
  13. <div data-role="content">
  14. <div data-role="controlgroup" data-type="horizontal">
  15. <p>水平分组:</p>
  16. <a href="#" data-role="button">按钮 1</a>
  17. <a href="#" data-role="button">按钮 2</a>
  18. <a href="#" data-role="button">按钮 3</a>
  19. </div><br>
  20. <div data-role="controlgroup" data-type="vertical">
  21. <p>垂直分组(默认):</p>
  22. <a href="#" data-role="button">按钮 1</a>
  23. <a href="#" data-role="button">按钮 2</a>
  24. <a href="#" data-role="button">按钮 3</a>
  25. </div>
  26. </div>
  27. <div data-role="footer">
  28. <h1>页脚文本</h1>
  29. </div>
  30. </div>
  31. </body>
  32. </html>
后退按钮

如需创建后退按钮,请使用 data-rel="back" 属性(会忽略锚的 href 值):
实例

  1. <a href="#" data-role="button" data-rel="back">返回</a>
其他属性
属性 描述
data-corners true/false 是否圆角
data-mini true/false 是否小型按钮
data-shadow true/false 是否有阴影

带图标的按钮

如需向您的按钮添加图标,请使用 data-icon 属性:

<a href="#anylink" data-role="button" data-icon="search">搜索</a>

提示:您也可以在 <button><input> 元素中使用 data-icon 属性。

jQuery Mobile 提供的一些可用图标

定位图标

您也能够规定图标被放置的位置:上、右、下或左。

请使用 data-iconpos 属性来规定位置:
图标位置:

  1. <a href="#link" data-role="button" data-icon="search" data-iconpos="top"></a>
  2. <a href="#link" data-role="button" data-icon="search" data-iconpos="right"></a>
  3. <a href="#link" data-role="button" data-icon="search" data-iconpos="bottom"></a>
  4. <a href="#link" data-role="button" data-icon="search" data-iconpos="left"></a>

jQuery Mobile 工具栏

标题栏

页眉通常会包含页眉标题/LOGO 或一到两个按钮(通常是首页、选项或搜索按钮)。

您可以在页眉中向左侧或/以及右侧添加按钮。

下面的代码,将向页眉标题文本的左侧添加一个按钮,并向右侧添加一个按钮:
实例

  1. <div data-role="header">
  2. <a href="#" data-role="button">首页</a>
  3. <h1>欢迎来到我的主页</h1>
  4. <a href="#" data-role="button">搜索</a>
  5. </div>

不过,如果您在 <h1> 元素之后放置按钮链接,那么它不会显示在文本右侧。如需向页眉标题的右侧添加按钮,请规定类名"ui-btn-right"
实例

  1. <div data-role="header">
  2. <h1>欢迎来到我的主页</h1>
  3. <a href="#" data-role="button" class="ui-btn-right">搜索</a>
  4. </div>

注意:在页眉可以放置1到2个按钮,而在页尾没有限制

页脚栏

与页眉相比,页脚更具伸缩性 - 它们更实用且多变,所以能够包含所需数量的按钮:
实例

  1. <div data-role="footer">
  2. <a href="#" data-role="button">转播到新浪微博</a>
  3. <a href="#" data-role="button">转播到腾讯微博</a>
  4. <a href="#" data-role="button">转播到 QQ 空间</a>
  5. </div>

注释:页脚与页眉的样式不同(它会减去一些内边距和空白,并且按钮不会居中)。如果要修正该问题,请在页脚设置类名 "ui-btn"
实例

  1. <div data-role="footer" class="ui-btn">

您也能够选择在页脚中水平还是垂直地组合按钮:
实例

  1. <div data-role="footer" class="ui-btn">
  2. <div data-role="controlgroup" data-type="horizontal">
  3. <a href="#" data-role="button" data-icon="plus">转播到新浪微博</a>
  4. <a href="#" data-role="button" data-icon="plus">转播到腾讯微博</a>
  5. <a href="#" data-role="button" data-icon="plus">转播到 QQ 空间</a>
  6. </div>
  7. </div>

定位页眉和页脚

放置页眉和页脚的方式有三种:

Inline - 默认。页眉和页脚与页面内容位于行内。
Fixed - 页面和页脚会留在页面顶部和底部。
Fullscreen - 与 fixed 类似;页面和页脚会留在页面顶部和底部,but also over the page content. It is also slightly see-through

请使用 data-position 属性来定位页眉和页脚:

Inline定位(默认)
  1. <div data-role="header" data-position="inline"></div>
  2. <div data-role="footer" data-position="inline"></div>
Fixed 定位(页眉不消失,页尾在使用时隐藏)
  1. <div data-role="header" data-position="fixed"></div>
  2. <div data-role="footer" data-position="fixed"></div>

Fullscreen 定位(完全沉浸式)

如果需要启用全面定位,请使用 data-position="fixed",并向该元素添加 data-fullscreen属性:

  1. <div data-role="header" data-position="fixed" data-fullscreen="true"></div>
  2. <div data-role="footer" data-position="fixed" data-fullscreen="true"></div>

fullscreen 对于照片、图像和视频非常理想。

jQuery Mobile 导航栏

导航栏由一组水平排列的链接构成,通常位于页眉或页脚内部。

默认地,导航栏中的链接会自动转换为按钮(无需 data-role="button")。

请使用 data-role="navbar" 属性来定义导航栏:
实例

  1. <div data-role="header">
  2. <div data-role="navbar">
  3. <ul>
  4. <li><a href="#anylink">首页</a></li>
  5. <li><a href="#anylink">页面二</a></li>
  6. <li><a href="#anylink">搜索</a></li>
  7. </ul>
  8. </div>
  9. </div>

按钮的宽度,默认地,与其内容一致。使用无序列表来均等地划分按钮:一个按钮占据 100% 的宽度,两个按钮各分享 50% 的宽度,三个按钮 33.3%,以此类推。不过,如果您在导航栏中规定了五个以上的按钮,那么它会弯折为多行

活动按钮

当导航栏中的链接被敲击时,会获得选取外观(按下)。

如需在不敲击链接时实现此外观,请使用 class="ui-btn-active":
实例

  1. <li><a href="#anylink" class="ui-btn-active">首页</a></li>

对于多个页面,您也许需要为每个按钮设置“被选”外观,以表示用户正在浏览该页面。如果要这么做,请向链接添加 "ui-state-persist" 类,以及 "ui-btn-active" 类:
实例

  1. <li><a href="#anylink" class="ui-btn-active ui-state-persist">首页</a></li>

jQuery Mobile 可折叠

可折叠的内容块

可折叠(Collapsibles)允许您隐藏或显示内容 - 对于存储部分信息很有用。

如需创建可折叠的内容块,请向某个容器分配 data-role="collapsible" 属性。在容器(div)中,添加一个标题元素(h1-h6),其后是您需要扩展的任意 HTML 标记:

  1. <div data-role="collapsible">
  2. <h1>点击我 - 我可以折叠!</h1>
  3. <p>我是可折叠的内容。</p>
  4. </div>

默认地,该内容是关闭的。如需在页面加载时扩展内容,请使用 data-collapsed="false":

  1. <div data-role="collapsible" data-collapsed="false">
  2. <h1>点击我 - 我可以折叠!</h1>
  3. <p>现在我默认是展开的。</p>
  4. </div>

可嵌套折叠

  1. <div data-role="collapsible">
  2. <h1>点击我 - 我可以折叠!</h1>
  3. <p>我是被展开的内容。</p>
  4. <div data-role="collapsible">
  5. <h1>点击我 - 我是嵌套的可折叠块!</h1>
  6. <p>我是嵌套的可折叠块中被展开的内容。</p>
  7. </div>
  8. </div>

可嵌套折叠任意次

可折叠集合

可折叠集合(Collapsible sets)指的是被组合在一起的可折叠块(常被称为手风琴)。当新块被打开时,所有其他块会关闭。

创建若干内容块,然后通过 data-role="collapsible-set" 用新容器包装这个可折叠块:

  1. <div data-role="collapsible-set">
  2. <div data-role="collapsible">
  3. <h1>点击我 - 我可以折叠!</h1>
  4. <p>我是被展开的内容。</p>
  5. </div>
  6. <div data-role="collapsible">
  7. <h1>点击我 - 我可以折叠!</h1>
  8. <p>我是被展开的内容。</p>
  9. </div>
  10. </div>

折叠块的其他内容

移除 collapsibles 上的圆角

data-inset="flase"

  1. <div data-role="collapsible" data-inset="false">
  2. <h1>这是没有圆角的可折叠内容块。</h1>
  3. <p>默认地,可折叠块带有包含外边距的圆角,使用 data-inset="false" 可去掉圆角。</p>
  4. </div>
使 collapsibles 更小巧

data-mini="true"

  1. <div data-role="collapsible" data-mini="true">
  2. <h1>点击我 - 我是可折叠的!</h1>
  3. <p>我是可折叠的内容。</p>
  4. </div>
改变 collapsibles(折叠标记) 的图标(默认是 + 和 -)

data-collapsed-icon="arrow-d"
data-expanded-icon="arrow-u"

  1. <div data-role="collapsible" data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u">
  2. <h1>data-collapsed-icon 规定按钮的图标。</h1>
  3. <p>data-expanded-icon 规定内容被展开时按钮的图标。</p>
  4. </div>

jQuery Mobile 布局网格

jQuery Mobile 布局网格

jQuery Mobile 提供了一套基于 CSS 的列布局方案。不过,一般不推荐在移动设备上使用列布局,这是由于移动设备的屏幕宽度所限。

但是有时您需要定位更小的元素,比如按钮或导航栏,就像在表格中那样并排。这时,列布局就恰如其分。

网格中的列是等宽的(总宽是 100%),无边框、背景、外边距或内边距。

可使用的布局网格有四种:ui-grid-a ui-grid-b ui-grid-c ui-grid-d
格式如下(以ui-grid-c为例)(ui-grid-c包含4个子容器)

  1. <div class="ui-grid-c">
  2. <div class="ui-block-a">
  3. <a href="#" data-role="button">第一列</a><br>
  4. </div>
  5. <div class="ui-block-b">
  6. <a href="#" data-role="button">第二列</a><br>
  7. </div>
  8. <div class="ui-block-c">
  9. <a href="#" data-role="button">第三列</a><br>
  10. </div>
  11. <div class="ui-block-d">
  12. <a href="#" data-role="button">第四列</a><br>
  13. </div>
  14. </div>

注意:每个子容器等宽

定制网格

通过使用 CSS 来定制列块:
实例

  1. <style>
  2. .ui-block-a,
  3. .ui-block-b,
  4. .ui-block-c
  5. {
  6. background-color: lightgray;
  7. border: 1px solid black;
  8. height: 100px;
  9. font-weight: bold;
  10. text-align: center;
  11. padding: 30px;
  12. }
  13. </style>

您也可以通过使用行内样式来定制块:

  1. <div class="ui-block-a" style="border: 1px solid black;"><span>Text..</span></div>

多行

在列中包含多个行也是可能的。

注释:ui-block-a-class 将始终创建新行:
实例

  1. <div class="ui-grid-b">
  2. <div class="ui-block-a"><span>Some Text</span></div>
  3. <div class="ui-block-b"><span>Some Text</span></div>
  4. <div class="ui-block-c"><span>Some Text</span></div>
  5. <div class="ui-block-a"><span>Some Text</span></div>
  6. <div class="ui-block-b"><span>Some Text</span></div>
  7. <div class="ui-block-a"><span>Some Text</span></div>
  8. </div>

jQuery Mobile 列表视图

jQuery Mobile 列表视图

jQuery Mobile 中的列表视图是标准的 HTML 列表:有序列表 (<ol>) 和无序列表 (<ul>)。

如需创建列表,请向 <ol><ul> 元素添加 data-role="listview"。如需使这些项目可点击,请在每个列表项(<li>)中规定链接:
实例

  1. <ol data-role="listview">
  2. <li><a href="#">列表项</a></li>
  3. </ol>
  4. <ul data-role="listview">
  5. <li><a href="#">列表项</a></li>
  6. </ul>

如需为列表添加圆角和外边距,请使用 data-inset="true" 属性:
实例

  1. <ul data-role="listview" data-inset="true">

默认地,列表中的列表项会自动转换为按钮(无需 data-role="button")。

列表分隔符

列表分隔符(List Dividers)用于把项目组织和组合为分类/节。

如需规定列表分隔符,请向

  • 元素添加 data-role="list-divider" 属性:
    实例

    1. <ul data-role="listview">
    2. <li data-role="list-divider">欧洲</li>
    3. <li><a href="#">法国</a></li>
    4. <li><a href="#">德国</a></li>
    5. </ul>

    如果您的列表是字母顺序的(比如通讯录),jQuery Mobile 自动添加恰当的分隔符,通过在 <ol><ul>元素上设置 data-autodividers="true"属性

    1. <ul data-role="listview" data-autodividers="true">
    2. <li><a href="#">Adam</a></li>
    3. <li><a href="#">Angela</a></li>
    4. <li><a href="#">Bill</a></li>
    5. <li><a href="#">Calvin</a></li>
    6. ...
    7. </ul>

    搜索过滤器

    如需在列表中添加搜索框,请使用 data-filter="true"属性:
    实例

    1. <ul data-role="listview" data-filter="true"></ul>

    默认地,搜索框中的文本是 "Filter items..."。

    如需修改默认文本,请使用 data-filter-placeholder 属性:
    实例

    1. <ul data-role="listview" data-filter="true" data-filter-placeholder="搜索姓名">

    jQuery Mobile 列表内容

    jQuery Mobile 列表缩略图

    对于大于 16x16px 的图像,请在链接中添加 <img>元素。

    jQuery Mobile 将自动把图像调整至 80x80px:
    实例

    1. <ul data-role="listview">
    2. <li><a href="#"><img src="chrome.png"></a></li>
    3. </ul>

    请使用标准 HTML 来填充带有信息的列表:以便于信息能正确的嵌进去

    1. <ul data-role="listview">
    2. <li>
    3. <a href="#">
    4. <img src="chrome.png">
    5. <h2>Google Chrome</h2>
    6. <p>Google Chrome 免费的开源 web 浏览器。发布于 2008 年。</p>
    7. </a>
    8. </li>
    9. </ul>

    jQuery Mobile 列表图标

    如需向您的列表添加 16x16px 的图标,请向 <img> 元素添加 class="ui-li-icon" 属性:
    实例

    1. <li><a href="#"><img src="us.png" alt="USA" class="ui-li-icon">USA</a></li>

    拆分按钮

    如需创建带有垂直分隔栏的拆分列表,请在

  • 元素内放置两个链接。

    jQuery Mobile 会自动为第二个链接添加蓝色箭头图标的样式,链接中的文本(如有)将在用户划过该图标时显示:
    实例

    1. <ul data-role="listview">
    2. <li>
    3. <a href="#"><img src="chrome.png"></a>
    4. <a href="#">Some Text</a>
    5. </li>
    6. </ul>

    计数泡沫

    计数泡沫用于显示与列表项相关的数目,例如邮箱中的消息:

    如需添加计数泡沫,请使用行内元素,比如<span>,设置 class "ui-li-count" 属性并添加数字:

    1. <ul data-role="listview">
    2. <li><a href="#">Inbox<span class="ui-li-count">335</span></a></li>
    3. <li><a href="#">Sent<span class="ui-li-count">123</span></a></li>
    4. <li><a href="#">Trash<span class="ui-li-count">7</span></a></li>
    5. </ul>

    更改列表项的默认链接图标

    直接修改data-icon即可

    1. <ul data-role="listview" data-inset="true">
    2. <li><a href="#">默认是右箭头</a></li>
    3. <li data-icon="plus"><a href="#">data-icon="plus"</a></li>
    4. <li data-icon="minus"><a href="#">data-icon="minus"</a></li>
    5. <li data-icon="delete"><a href="#">data-icon="delete"</a></li>
    6. <li data-icon="info"><a href="#">data-icon="info"</a></li>
    7. <li data-icon="false"><a href="#">data-icon="false"</a></li>
    8. </ul>

    可折叠列表

    data-role="collapsible"使用在标签中即可

    1. <div data-role="content">
    2. <div data-role="collapsible">
    3. <h4>A</h4>
    4. <ul data-role="listview">
    5. <li><a href="#">Adam</a></li>
    6. <li><a href="#">Angela</a></li>
    7. </ul>
    8. </div>

    制作日历

    1. <h2>我的日历</h2>
    2. <ul data-role="listview" data-inset="true">
    3. <li data-role="list-divider">星期三, 1 月 2 日, 2013 <span class="ui-li-count">2</span></li>
    4. <li><a href="#">
    5. <h2>医生</h2>
    6. <p><b>To Peter Griffin</b></p>
    7. <p>Well, Mr. Griffin, I've looked into physical results.</p>
    8. <p>Ah, Mr. Griffin, I'm not quite sure how to say this. Kim Bassinger? Bass singer? Bassinger?</p>
    9. <p>But now, onto the cancer</p>
    10. <p>You are a Cancer, right? You were born in July? Now onto these test results.</p>
    11. <p class="ui-li-aside">Re: Appointment</p></a>
    12. </li>
    13. <li><a href="#">
    14. <h2>Glen Quagmire</h2>
    15. <p>Remember me this weekend!</p>
    16. <br>
    17. <p>- giggity giggity goo</p>
    18. <p class="ui-li-aside">Re: Camping</p></a>
    19. </li>

    jQuery Mobile 表单

    jQuery Mobile 会自动为 HTML 表单添加优异的便于触控的外观。

    jQuery Mobile 表单结构

    jQuery Mobile 使用 CSS 来设置 HTML 表单元素的样式,以使其更有吸引力更易用。

    在 jQuery Mobile 中,您可以使用以下表单控件:

    文本框
    搜索框
    单选框
    复选框
    选择菜单
    滑动条
    翻转切换开关
    

    当您与 jQuery Mobile 表单打交道时,应该了解以下信息:

    <form> 元素必须设置 method 和 action 属性
    每个表单元素必须设置唯一的 "id" 属性。该 id 在站点的页面中必须是唯一的。这是因为 jQuery Mobile 的单页面导航模型允许许多不同的“页面”同时呈现。
    每个表单元素必须有一个标记(label)。请设置 label 的 for 属性来匹配元素的 id。
    

    实例

    1. <form method="post" action="demoform.asp">
    2. <label for="fname">First name:</label>
    3. <input type="text" name="fname" id="fname">
    4. </form>

    如需隐藏 label,请使用类 ui-hidden-accessible。这很常用,当您需要元素的 placeholder 属性充当 label 时:
    实例

    1. <form method="post" action="demoform.asp">
    2. <label for="fname" class="ui-hidden-accessible">姓名:</label>
    3. <input type="text" name="fname" id="fname" placeholder="姓名...">
    4. </form>

    域容器

    如果需要 label 和表单元素在宽屏幕上显示正常,请用带有 data-role="fieldcontain"属性的 <div><fieldset> 元素来包装 label 或表单元素:
    实例

    1. <form method="post" action="demoform.asp">
    2. <div data-role="fieldcontain">
    3. <label for="fname">First name:</label>
    4. <input type="text" name="fname" id="fname">
    5. <label for="lname">Last name:</label>
    6. <input type="text" name="lname" id="lname">
    7. </div>
    8. </form>

    注意:fieldcontain 属性基于页面宽度来设置 label 和表单控件的样式。当页面宽度大于 480px 时,它会自动将 label 与表单控件放置于同一行。当小于 480px 时,label 会被放置于表单元素之上。

    如需避免 jQuery Mobile 自动为可点击元素设置样式,请使用 data-role="none" 属性:
    实例

    1. <label for="fname">First name:</label>
    2. <input type="text" name="fname" id="fname" data-role="none">

    jQuery Mobile 表单输入元素

    jQuery Mobile 文本输入

    输入字段是通过标准的 HTML 元素编写的,jQuery Mobile 会为它们设置专门针对移动设备的美观易用的样式。您还可以使用新的 HTML5 <input> 类型:

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
    5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    7. </head>
    8. <body>
    9. <div data-role="page">
    10. <div data-role="header">
    11. <h1>文本输入</h1>
    12. </div>
    13. <div data-role="content">
    14. <form method="post" action="demoform.asp">
    15. <div data-role="fieldcontain">
    16. <label for="fullname">全名:</label>
    17. <input type="text" name="fullname" id="fullname">
    18. <label for="bday">生日:</label>
    19. <input type="date" name="bday" id="bday">
    20. <label for="email">电邮:</label>
    21. <input type="email" name="email" id="email" placeholder="您的邮箱地址..">
    22. </div>
    23. <input type="submit" data-inline="true" value="提交">
    24. </form>
    25. </div>
    26. </div>
    27. </body>
    28. </html>

    请使用 placeholder 来规定简短的提示,以描述输入字段的预期值:
    <input placeholder="sometext">

    文本框

    请使用 <textarea> 来实现多行文本输入。
    注释:文本框会自动扩大,以适应您输入的文本行。

    1. <form method="post" action="demoform.asp">
    2. <div data-role="fieldcontain">
    3. <label for="info">Additional Information:</label>
    4. <textarea name="addinfo" id="info"></textarea>
    5. </div>
    6. </form>

    搜索框

    输入类型 type="search" 是 HTML5 中的新类型,用于定义供输入搜索词的文本字段:

    1. <form method="post" action="demoform.asp">
    2. <div data-role="fieldcontain">
    3. <label for="search">Search:</label>
    4. <input type="search" name="search" id="search">
    5. </div>
    6. </form>

    单选按钮

    当用户只选择有限数量选项中的一个时,会用到单选按钮。
    如需创建一套单选按钮,请添加 type="radio"input元素以及相应的 label。在 <fieldset> 元素中包装单选按钮。您也可以增加一个 <legend> 元素来定义<fieldset> 的标题。
    提示:请用data-role="controlgroup" 属性来组合这些按钮:

    1. <form method="post" action="demoform.asp">
    2. <fieldset data-role="controlgroup">
    3. <legend>Choose your gender:</legend>
    4. <label for="male">Male</label>
    5. <input type="radio" name="gender" id="male" value="male">
    6. <label for="female">Female</label>
    7. <input type="radio" name="gender" id="female" value="female">
    8. </fieldset>
    9. </form>

    复选框

    当用户选择有限数量选项中的一个或多个选项时,会用到复选框:
    和单选我唯一的不同是:复选是input type="checkbox"单选是input type="radio"

    1. <form method="post" action="demoform.asp">
    2. <fieldset data-role="controlgroup">
    3. <legend>Choose as many favorite colors as you'd like:</legend>
    4. <label for="red">Red</label>
    5. <input type="checkbox" name="favcolor" id="red" value="red">
    6. <label for="green">Green</label>
    7. <input type="checkbox" name="favcolor" id="green" value="green">
    8. <label for="blue">Blue</label>
    9. <input type="checkbox" name="favcolor" id="blue" value="blue">
    10. </fieldset>
    11. </form>

    为选项设置预选

    如果您希望“预选”其中一个按钮,请使用 HTML<input> 标签的 checked 属性:

    1. <input type="radio" checked>
    2. <input type="checkbox" checked>

    jQuery Mobile 表单选择菜单

    <select> 元素创建带有若干选项的下拉菜单。
    <select> 元素中的 <option> 元素定义列表中的可用选项:

    1. <form method="post" action="demoform.asp">
    2. <fieldset data-role="fieldcontain">
    3. <label for="day">选择日期</label>
    4. <select name="day" id="day">
    5. <option value="mon">星期一</option>
    6. <option value="tue">星期二</option>
    7. <option value="wed">星期三</option>
    8. </select>
    9. </fieldset>
    10. </form>

    如果列表中包含了一长列相关的选项,请在<select>中使用 <optgroup> 元素:

    1. <select name="day" id="day">
    2. <optgroup label="Weekdays">
    3. <option value="mon">Monday</option>
    4. <option value="tue">Tuesday</option>
    5. <option value="wed">Wednesday</option>
    6. </optgroup>
    7. <optgroup label="Weekends">
    8. <option value="sat">Saturday</option>
    9. <option value="sun">Sunday</option>
    10. </optgroup>
    11. </select>

    如果您希望在所有移动设备上显示一致外观的选择菜单,请使用 jQuery 的自定义选择菜单(说实话,我并不认为这种样式好看),data-native-menu="false"属性:

    1. <select name="day" id="day" data-native-menu="false">

    Multiple Selections

    如需在选择菜单中选取多个选项,请在 <select> 元素中使用 multiple 属性:

    1. <select name="day" id="day" multiple data-native-menu="false">

    注意:在这里multiple相当于multiple="multiple"

    预选选项

    add selected

    1. <div data-role="content">
    2. <form method="post" action="demoform.asp">
    3. <fieldset data-role="fieldcontain">
    4. <label for="day">选择日期</label>
    5. <select name="day" id="day">
    6. <option value="mon">Monday</option>
    7. <option value="tue">Tuesday</option>
    8. <option value="wed" selected>Wednesday</option>
    9. <option value="thu">Thursday</option>
    10. <option value="fri">Friday</option>
    11. <option value="sat">Saturday</option>
    12. <option value="sun">Sunday</option>
    13. </select>
    14. </fieldset>
    15. <input type="submit" data-inline="true" value="提交">
    16. </form>
    17. </div>
    18. </div>

    水平地组合选择菜单

    1. <div data-role="content">
    2. <form method="post" action="demoform.asp">
    3. <div data-role="fieldcontain">
    4. <fieldset data-role="controlgroup" data-type="horizontal">
    5. <legend>安排会议:</legend>
    6. <label for="day">选择日期:</label>
    7. <select name="day" id="day">
    8. <option value="mon">Monday</option>
    9. <option value="tue">Tuesday</option>
    10. <option value="wed">Wednesday</option>
    11. <option value="thu">Thursday</option>
    12. <option value="fri">Friday</option>
    13. <option value="sat">Saturday</option>
    14. <option value="sun">Sunday</option>
    15. </select>
    16. <label for="time">选择时间:</label>
    17. <select name="time" id="time">
    18. <option value="08">08:00</option>
    19. <option value="09">09:00</option>
    20. <option value="10">10:00</option>
    21. <option value="11">11:00</option>
    22. <option value="12">12:00</option>
    23. <option value="13">13:00</option>
    24. <option value="14">14:00</option>
    25. <option value="15">15:00</option>
    26. <option value="16">16:00</option>
    27. </select>
    28. </fieldset>
    29. </div>
    30. <input type="submit" data-inline="true" value="提交">
    31. </form>
    32. </div>
    33. </div>

    jQuery Mobile 表单 - 滑块

    jQuery Mobile 滑块控件

    滑块允许您从一定范围内的数字中选取值:
    如需创建滑块,请使用 <input type="range">

    1. <form method="post" action="demoform.asp">
    2. <div data-role="fieldcontain">
    3. <label for="points">Points:</label>
    4. <input type="range" name="points" id="points" value="50" min="0" max="100">
    5. </div>
    6. </form>

    使用下列属性来规定限定:
    max - 规定允许的最大值
    min - 规定允许的最小值
    step - 规定合法的数字间隔
    value - 规定默认值

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
    5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    7. </head>
    8. <body>
    9. <div data-role="page">
    10. <div data-role="header">
    11. <h1>滑块控件</h1>
    12. </div>
    13. <div data-role="content">
    14. <form method="post" action="demoform.asp">
    15. <div data-role="fieldcontain">
    16. <label for="points">Points:</label>
    17. <input id="value" type="range" name="points" id="points" value="50" min="0" max="100">
    18. </div>
    19. <input type="submit" onclick="demo()" data-inline="true" value="提交">
    20. </form>
    21. </div>
    22. </div>
    23. <script>
    24. function demo(){
    25. var val = document.getElementById("value").value;
    26. alert(val);
    27. }
    28. </script>
    29. </body>
    30. </html>

    use data-higtlight to control heighlight

    1. <input type="range" data-hightlight="true">

    切换开关

    切换开关常用于开/关或对/错按钮:

    如需创建切换,请使用 data-role="slider"<select> 元素,并添加两个 <option> 元素:

    1. <form method="post" action="demoform.asp">
    2. <div data-role="fieldcontain">
    3. <label for="switch">Toggle Switch:</label>
    4. <select name="switch" id="switch" data-role="slider">
    5. <option value="on">On</option>
    6. <option value="off">Off</option>
    7. </select>
    8. </div>
    9. </form>

    jQuery Mobile 主题

    jQuery Mobile 主题

    jQuery Mobile 提供了五种不同的样式主题,从 "a" 到 "e" - 每种主题带有不同颜色的按钮、栏、内容块,等等。jQuery Mobile 中的一种主题由多种可见的效果和颜色构成。
    如需定制应用程序的外观,请使用data-theme 属性,并为该属性分配一个字母:

    1. <div data-role="page" data-theme="a|b|c|d|e">

    默认地,jQuery Mobile 为页眉和页脚使用 "a" 主题,为页眉内容使用 "c" 主题(亮灰)。不过,您能够自如地对主题进行混合

    主题化的对话框

    data-overlay-theme控制背景颜色

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
    5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    7. </head>
    8. <body>
    9. <div data-role="page" id="pageone">
    10. <div data-role="header">
    11. <h1>欢迎来到我的主页</h1>
    12. </div>
    13. <div data-role="content">
    14. <p>这是一张标准页面。</p>
    15. <a href="#pagetwo" data-rel="dialog">转到主题化的对话页面</a>
    16. </div>
    17. <div data-role="footer">
    18. <h1>页脚文本</h1>
    19. </div>
    20. </div>
    21. <div data-role="page" id="pagetwo" data-overlay-theme="b">
    22. <div data-role="header" data-theme="b">
    23. <h1>我是主题化的对话框</h1>
    24. </div>
    25. <div data-role="content" data-theme="a">
    26. <p>data-overlay-theme 属性规定对话框出现在其上的页面的背景色。</p>
    27. <a href="#pageone">转到页面一</a>
    28. </div>
    29. <div data-role="footer" data-theme="e">
    30. <h1>页脚文本</h1>
    31. </div>
    32. </div>
    33. </body>
    34. </html>

    添加新主题

    jQuery Mobile 同时允许您向移动页面添加新主题。
    请通过编辑 CSS 文件(如已下载 jQuery Mobile)来添加或编辑新主题。只需拷贝一段样式,并用字母名(f-z)来对类进行重命名,然后调整为您喜欢的颜色和字体即可。
    您也可以通过在 HTML 文档中使用主题类来添加新样式 - 为工具条添加类 ui-bar-(a-z),并为内容添加类 ui-body-(a-z)

    1. <style>
    2. .ui-bar-f
    3. {
    4. color:green;
    5. background-color:yellow;
    6. }
    7. .ui-body-f
    8. {
    9. font-weight:bold;
    10. color:purple;
    11. }
    12. </style>

    jQuery Mobile 事件

    jQuery Mobile 事件

    您能够在 jQuery Mobile 中使用任何标准的 jQuery 事件
    此外,jQuery Mobile 还提供若干种为移动浏览定制的事件:
    触摸事件 - 当用户触摸屏幕时触发(敲击和滑动)
    滚动事件 - 当上下滚动时触发
    方向事件 - 当设备垂直或水平旋转时触发
    页面事件 - 当页面被显示、隐藏、创建、加载以及/或卸载时触发

    在 jQuery Mobile 中,我们使用 pageinit 事件,该事件在页面已初始化并完善样式设置后发生(当第一个界面初始化后触发pageinit事件,若进入第二个页面后,再回到第一个页面,不会触发pageinit事件)
    第二个参数指向 ("#pageone") 指向页面的 idpoints to the id of the page to specify the events for:
    jQuery Mobile pageinit 事件

    1. <script>
    2. $(document).on("pageinit","#pageone",function(){
    3. // 此处是 jQuery 事件...
    4. });
    5. </script>

    jQuery on()方法用于添加事件处理程序。

    jQuery Mobile Touch 事件

    Touch 事件在用户触摸屏幕(页面)时触发。
    在桌面环境下等价于鼠标点击。

    jQuery Mobile Tap

    tap 事件在用户敲击某个元素时触发。
    下面的例子当 <p>元素上触发 tap 事件时,隐藏当前 <p>元素:

    1. $("p").on("tap",function(){
    2. $(this).hide();
    3. });
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
    5. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    6. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    7. <script>
    8. $(document).on("pageinit","#pageone",function(){
    9. $("p").on("tap",function(){
    10. $(this).hide();
    11. });
    12. });
    13. </script>
    14. </head>
    15. <body>
    16. <div data-role="page" id="pageone">
    17. <div data-role="header">
    18. <h1>tap 事件</h1>
    19. </div>
    20. <div data-role="content">
    21. <p>敲击我,我会消失。</p>
    22. <p>敲击我,我会消失。</p>
    23. <p>敲击我,我也会消失。</p>
    24. </div>
    25. <div data-role="footer">
    26. <h1>页脚文本</h1>
    27. </div>
    28. </div>
    29. </body>
    30. </html>
    taphold事件

    同样的 taphold 事件在用户持续点击某个元素(1秒)时触发,使用方法和tap事件相同。

    1. $("p").on("taphold",function(){
    2. $(this).hide();
    3. });
    jQuery Mobile Swipe

    swipe 事件在用户在某个元素上水平滑动超过 30px 时被触发:

    1. $("p").on("swipe",function(){
    2. $("span").text("Swipe detected!");
    3. });
    jQuery Mobile Swipeleft

    swipeleft 事件在用户在某个元素上从左滑动超过 30px 时被触发:

    1. $("p").on("swipeleft",function(){
    2. alert("You swiped left!");
    3. });
    jQuery Mobile Swiperight

    swiperight 事件在用户在某个元素上从右滑动超过 30px 时被触发:

    1. $("p").on("swiperight",function(){
    2. alert("You swiped right!");
    3. });

    jQuery Mobile 滚动事件

    jQuery Mobile 提供两种滚动事件:在滚动开始和当滚动结束。

    jQuery Mobile Scrollstart

    scrollstart 事件在用户开始滚动页面时被触发:

    1. $(document).on("scrollstart",function(){
    2. alert("开始滚动!");
    3. });

    iOS 设备会在滚动事件发生时冻结 DOM 操作,这意味着当用户滚动时无法改变任何事物。

    jQuery Mobile Scrollstop

    scrollstop 事件在用户停止滚动页面时被触发:

    1. $(document).on("scrollstop",function(){
    2. alert("结束滚动!");
    3. });

    jQuery Mobile 方向事件

    jQuery Mobile orientationchange 事件

    orientationchange 事件在用户垂直或水平旋转移动设备时被触发。
    Mobile
    如需使用 orientationchange 事件,请把它添加到 window 对象:

    1. $(window).on("orientationchange",function(){
    2. alert("方向已改变!");
    3. });

    callback 函数可以设置一个参数,即 event 对象,它会返回移动设备的方向:"portrait" (设备被握持的方向是垂直的)或 "landscape" (设备被握持的方向是水平的):

    1. $(window).on("orientationchange",function(event){
    2. alert("方向是:" + event.orientation);
    3. });

    jQuery Mobile 页面事件

    在 jQuery Mobile 中与页面打交道的事件被分为四类:
    Page Initialization - 在页面创建前,当页面创建时,以及在页面初始化之后
    Page Load/Unload - 当外部页面加载时、卸载时或遭遇失败时
    Page Transition - 在页面过渡之前和之后
    Page Change - 当页面被更改,或遭遇失败时
    如需关于所有 jQuery Mobile 事件的完整信息,请访问我们的 jQuery Mobile 事件参考手册

    jQuery Mobile Initialization 事件

    当 jQuery Mobile 中的一张典型页面进行初始化时,它会经历三个阶段:
    在页面创建前
    页面创建
    页面初始化
    每个阶段触发的事件都可用于插入或操作代码。

    事件 描述
    pagebeforecreat 当页面即将初始化,并且在 jQuery Mobile 已开始增强页面之前,触发该事件。
    pagecreat 当页面已创建,但增强完成之前,触发该事件。
    pageinit 当页面已初始化,并且在 jQuery Mobile 已完成页面增强之后,触发该事件。
    1. $(document).on("pagebeforecreate",function(event){
    2. alert("触发 pagebeforecreate 事件!");
    3. });
    4. $(document).on("pagecreate",function(event){
    5. alert("触发 pagecreate 事件!");
    6. });
    7. $(document).on("pageinit",function(event){
    8. alert("触发 pageinit 事件!")
    9. });

    jQuery Mobile Load 事件

    页面加载事件属于外部页面。
    无论外部页面何时载入 DOM,将触发两个事件。第一个是 pagebeforeload,第二个是 pageload (成功)或 pageloadfailed(失败)。
    下表中解释了这些事件:

    事件 描述
    pagebeforeload 在任何页面加载请求作出之前触发。
    pagepageload 在页面已成功加载并插入 DOM 后触发。
    pagepageloadfailed 如果页面加载请求失败,则触发该事件。默认地,将显示 "Error Loading Page" 消息

    jQuery Mobile 过渡事件

    我们还可以在从一页过渡到下一页时使用事件。
    页面过渡涉及两个页面:一张“来”的页面和一张“去”的页面 - 这些过渡使当前活动页面(“来的”页面)到新页面(“去的”页面的改变过程变得更加动感。

    事件 描述
    pagebeforeshow 在“去的”页面触发,在过渡动画开始前。
    pageshow 在“去的”页面触发,在过渡动画完成后。
    pagebeforehide 在“来的”页面触发,在过渡动画开始前。
    pagehide 在“来的”页面触发,在过渡动画完成后。

    path.get() 确定URL中的目录部分

    jQuery.mobile.path.get( url )

    1. <script>
    2. $(document).ready(function() {
    3. $( ".myButton" ).on( "click", function() {
    4. var dirName = $.mobile.path.get( $( this ).attr( "value" ) );
    5. $( "#myResult" ).html( String( dirName ) );
    6. })
    7. });
    8. </script>
  • 添加新批注
    在作者公开此批注前,只有你和作者可见。
    回复批注