jQuery Mobile 教程
概述 jQuery Mobile 面向触屏移动 Web 应用的声明式框架定位、基础页面骨架(data-role="page"/"header"/"content"/"footer")、历史演进与 OpenJS 归档现状,并离线核验 DOM 角色结构及现代 HTML5 语义对照。
学完这一章,你能够
- 概述 jQuery Mobile 面向触屏移动 Web 应用的声明式框架定位、基础页面骨架(data-role="page"/"header"/"content"/"footer")、历史演进与 OpenJS 归档现状,并离线核验 DOM 角色结构及现代 HTML5 语义对照。
框架定位与生命周期说明
jQuery Mobile 是一套面向智能手机与平板电脑的移动端 Web 前端框架,依托 HTML5 自定义属性与 CSS3 样式规则,仅需极少量脚本即可快速搭建适配触屏交互的页面布局。需要明确技术生命周期:OpenJS Foundation 已于 2021 年将 jQuery Mobile 正式归档(停止维护,最终稳定版为 1.4.5);当代移动端 Web 开发普遍采用原生响应式 CSS(Flexbox 与 CSS Grid)、HTML5 `<dialog>`/`<details>` 以及现代组件框架替代,学习本教程的核心价值在于维护既有历史项目与理解声明式移动 UI 设计思想。
基础页面骨架与离线对照
一个最简 jQuery Mobile 页面通过 `<div data-role="page" id="pageone">` 定义视图边界,内部依次放置页眉(`data-role="header"`)、正文区(1.3 常见写法为 `data-role="content"`,1.4 推荐改用 `data-role="main" class="ui-content"`)与页脚(`data-role="footer"`)。与传统在线编辑预览不同,本站离线实验利用 `lxml.html` 对两代页面骨架及现代原生 HTML5 `<header>`/`<main>`/`<footer>` 语义地标进行了结构等价核验。
本系列后续章节将按组件模块依次拆解页面、过渡、按钮、图标、弹窗、工具栏、导航栏、面板、可折叠块、表格、网格与列表视图,并配套系统化的 `data-*` 属性与 CSS 类参考条目,便于在排查旧系统代码或做技术迁移时快速检索。
运行 jQuery Mobile 基础页面 data-role 骨架与 HTML5 语义地标离线核验
展开查看收起
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 01: tutorial."""
import json
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"
def run_cases():
cases = []
# Case 1: Basic single-page data-role skeleton (1.3 content vs 1.4 main)
doc_v13 = html.fragment_fromstring(
"""
<div data-role="page" id="pageone">
<div data-role="header"><h1>页面标题</h1></div>
<div data-role="content"><p>页面内容</p></div>
<div data-role="footer"><h1>页面底部内容</h1></div>
</div>
"""
)
doc_v14 = html.fragment_fromstring(
"""
<div data-role="page" id="pageone">
<div data-role="header"><h1>页面标题</h1></div>
<div data-role="main" class="ui-content"><p>页面内容</p></div>
<div data-role="footer"><h1>页面底部内容</h1></div>
</div>
"""
)
actual_1 = {
"page_id": doc_v13.get("id"),
"v13_child_roles": [el.get("data-role") for el in doc_v13.xpath("./div[@data-role]")],
"v14_child_roles": [el.get("data-role") for el in doc_v14.xpath("./div[@data-role]")],
"v14_main_has_ui_content": "ui-content" in doc_v14.xpath("./div[@data-role='main']")[0].get("class", "").split(),
}
expected_1 = {
"page_id": "pageone",
"v13_child_roles": ["header", "content", "footer"],
"v14_child_roles": ["header", "main", "footer"],
"v14_main_has_ui_content": True,
}
cases.append({
"name": "basic_page_data_roles",
"summary": "Verify jQuery Mobile page container id and header/content(main)/footer data-role hierarchy.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Semantic HTML5 modern equivalent mapping check
modern_doc = html.fromstring(
"""
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body>
<header><h1>页面标题</h1></header>
<main><p>页面内容</p></main>
<footer><p>页面底部内容</p></footer>
</body>
</html>
"""
)
actual_2 = {
"has_viewport": bool(modern_doc.xpath("//meta[@name='viewport' and contains(@content, 'width=device-width')]")),
"landmarks": [el.tag for el in modern_doc.xpath("//body/*")],
"h1_text": modern_doc.xpath("string(//header/h1)"),
}
expected_2 = {
"has_viewport": True,
"landmarks": ["header", "main", "footer"],
"h1_text": "页面标题",
}
cases.append({
"name": "modern_html5_landmark_mapping",
"summary": "Verify modern native HTML5 header/main/footer landmarks and viewport meta equivalent.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Data-role attribute inventory check across core widget families
sample_app = html.fragment_fromstring(
"""
<div data-role="page" id="home">
<div data-role="header"><h1>移动端概览</h1></div>
<div data-role="main" class="ui-content">
<ul data-role="listview"><li><a href="#detail">示例条目</a></li></ul>
</div>
<div data-role="footer"><h4>页脚导航</h4></div>
</div>
"""
)
roles = sample_app.xpath(".//@data-role")
actual_3 = {
"root_role": sample_app.get("data-role"),
"nested_roles": roles,
"link_targets": sample_app.xpath(".//a/@href"),
}
expected_3 = {
"root_role": "page",
"nested_roles": ["page", "header", "main", "listview", "footer"],
"link_targets": ["#detail"],
}
cases.append({
"name": "data_role_widget_inventory",
"summary": "Inspect nested data-role declarations and internal anchor links inside a jQM page.",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
return cases
def main():
print(json.dumps({"version": VERSION, "cases": run_cases()}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
jQuery Mobile 1.4 将主体容器的视觉留白(内边距)拆分为纯 CSS 类 ui-content,并以 data-role="main" 对应 WAI-ARIA main 地标角色,减少脚本初始化开销并提升语义可访问性。