蓝色天空网mbsky.com · 知识 · 实践 · 创造jQuery Mobile · 本地阅读预览
教程 / 移动开发 / jQuery Mobile

jQuery Mobile 教程

概述 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 语义地标离线核验

python · 阅读源码
"""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()
预期结果 · 待实际运行核验
  • 确认 #pageone 视图完整包含 header/content(main)/footer 角色,1.4 主体区携带 ui-content 类,并核对现代 HTML5 语义标签及 viewport 元标签映射。
运行 jQuery Mobile 基础页面 data-role 骨架与 HTML5 语义地标离线核验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:4ecb3373b75c114d2eb2cba2ad2a1a73dfab64f3bf90e082968abaf592ff819f

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载jQuery Mobile 教程实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:c02aae867f8d335c94b19deb0c54b3bcb8fd6385eb6b5095ce395ef9f1cabea5

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 jQuery Mobile 1.4 中,定义页面主体内容容器时为什么推荐使用 data-role="main" class="ui-content" 替代早期的 data-role="content"?

展开参考解答

jQuery Mobile 1.4 将主体容器的视觉留白(内边距)拆分为纯 CSS 类 ui-content,并以 data-role="main" 对应 WAI-ARIA main 地标角色,减少脚本初始化开销并提升语义可访问性。

规范与官方依据