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

jQuery Mobile 导航栏

解析 jQuery Mobile 水平导航栏(data-role="navbar")基于无序列表的等宽栅格与超过 5 项折行规则、data-icon 与 data-iconpos 图标配置、ui-btn-active / ui-state-persist 激活持久化状态及多容器复用场景。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

导航栏定义与等宽自动折行规则

导航栏(Navbar)由一组水平排列的链接构成,常见于头部栏或尾部栏内。只需在容器添加 `data-role="navbar"` 并嵌套标准 `<ul><li><a href="...">` 列表,内部链接即自动增强为等宽按钮(无需再写 `data-role="button"`)。当按钮数量为 1 至 5 个时,框架会按列数均分 100% 宽度(1 项 100%、2 项各 50%、3 项各 33.3% 依此类推);一旦超过 5 个按钮,则自动拆分为两列多行排布。

导航按钮图标与容器级图标定位

导航栏中的按钮使用 `data-icon="<name>"` 声明图标(取值与 `ui-icon-<name>` 保持一致,如 `home`、`arrow-r`、`search`,完整列表参阅图标参考手册)。图标相对文字的方位默认置于上方(`data-iconpos="top"`),如需调整为右侧、底部或左侧对齐,应直接在 `data-role="navbar"` 容器上统一设置 `data-iconpos="top|right|bottom|left"`。

激活高亮、跨页持久化与常见放置位置

点击导航链接时按钮会自动获得按下高亮态;若要在初始加载时标示当前页,可给对应链接添加 `class="ui-btn-active"`。在多页文档中,为了在页面来回切换后仍保持目标标签的选中外观,须同时叠加 `class="ui-btn-active ui-state-persist"`。除头部外,导航栏同样可放进 `data-role="main"`(或旧版 `data-role="content"`)正文区、尾部栏(配合 `data-iconpos` 定制图标方位)以及超过 5 项的多行入口区。鉴于 jQuery Mobile 已归档停更,现代移动导航栏通常使用原生 `<nav><ul>` 结合 CSS Flexbox/Grid 与 `aria-current="page"` 构建。

导航栏宽度均分与多行折行规则

主题栅格分配机制
1–5 项单行均分,6 项及以上拆行导航栏借助无序列表按 100%/N 自动均分 1–5 个按钮宽度;超过 5 个按钮时切换为每行两列(50%)的多行网格。

导航栏常用 data-icon 示例对照

属性写法图标语义对应图标类与说明
属性语法(data-icon)导航项图标语义对应 1.4 图标类名(ui-icon-*)
data-icon="home"首页图标等价于 ui-icon-home
data-icon="arrow-r"右侧箭头图标等价于 ui-icon-arrow-r
data-icon="search"搜索图标等价于 ui-icon-search

导航栏 data-iconpos 图标位置取值表

属性取值对齐方向离线核验
data-iconpos="top"图标位于文字上方(默认)navbar_icons_and_positions
data-iconpos="right"图标位于文字右侧navbar_icons_and_positions
data-iconpos="bottom"图标位于文字下方navbar_icons_and_positions
data-iconpos="left"图标位于文字左侧navbar_icons_and_positions

导航按钮默认图标方位提示

主题默认对齐值
默认顶部图标布局未在 data-role="navbar" 上设置 data-iconpos 时,导航按钮默认采用 data-iconpos="top" 将图标置于文字上方。

运行导航栏等宽/折行规则、data-icon/data-iconpos 与持久激活态离线核验

python · 阅读源码
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 10: navbars."""
import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"


def navbar_column_plan(item_count: int) -> dict:
    if item_count <= 5:
        return {"columns_per_row": item_count, "wraps_to_two_col_grid": False, "width_pct": round(100.0 / item_count, 1)}
    return {"columns_per_row": 2, "wraps_to_two_col_grid": True, "width_pct": 50.0}


def run_cases():
    cases = []

    # Case 1: Navbar structure inside header & equal-width / >5 wrapping rule
    doc1 = html.fragment_fromstring(
        """
        <div data-role="header">
          <div data-role="navbar">
            <ul>
              <li><a href="#anylink">首页</a></li>
              <li><a href="#anylink">页面二</a></li>
              <li><a href="#anylink">搜索</a></li>
            </ul>
          </div>
        </div>
        """
    )
    items = doc1.xpath(".//div[@data-role='navbar']/ul/li/a")
    actual_1 = {
        "navbar_item_count": len(items),
        "plan_3_items": navbar_column_plan(len(items)),
        "plan_6_items": navbar_column_plan(6),
    }
    expected_1 = {
        "navbar_item_count": 3,
        "plan_3_items": {"columns_per_row": 3, "wraps_to_two_col_grid": False, "width_pct": 33.3},
        "plan_6_items": {"columns_per_row": 2, "wraps_to_two_col_grid": True, "width_pct": 50.0},
    }
    cases.append({
        "name": "navbar_structure_and_width_rules",
        "summary": "Verify data-role='navbar' ul/li/a layout, equal width division up to 5 items, and multi-row wrap beyond 5.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Navbar data-icon values and data-iconpos placement on navbar container
    doc2 = html.fragment_fromstring(
        """
        <div>
          <div data-role="navbar" data-iconpos="top">
            <ul>
              <li><a href="#p1" data-icon="home">首页</a></li>
              <li><a href="#p2" data-icon="arrow-r">下一页</a></li>
              <li><a href="#p3" data-icon="search">搜索</a></li>
            </ul>
          </div>
          <div data-role="navbar" data-iconpos="right"></div>
          <div data-role="navbar" data-iconpos="bottom"></div>
          <div data-role="navbar" data-iconpos="left"></div>
        </div>
        """
    )
    actual_2 = {
        "icons": doc2.xpath(".//a/@data-icon"),
        "iconpos_modes": doc2.xpath("./div[@data-role='navbar']/@data-iconpos"),
        "default_iconpos": "top",
    }
    expected_2 = {
        "icons": ["home", "arrow-r", "search"],
        "iconpos_modes": ["top", "right", "bottom", "left"],
        "default_iconpos": "top",
    }
    cases.append({
        "name": "navbar_icons_and_positions",
        "summary": "Verify data-icon attributes on navbar anchors and data-iconpos (top/right/bottom/left) on navbar containers.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Active state (ui-btn-active) and multi-page persistence (ui-state-persist)
    doc3 = html.fragment_fromstring(
        """
        <div data-role="navbar">
          <ul>
            <li><a href="#pageone" class="ui-btn-active">当前激活</a></li>
            <li><a href="#pagetwo" class="ui-btn-active ui-state-persist">跨页持久激活</a></li>
          </ul>
        </div>
        """
    )
    a1, a2 = doc3.xpath(".//a")
    actual_3 = {
        "first_classes": a1.get("class").split(),
        "second_classes": a2.get("class").split(),
        "persists_across_pages": {"ui-btn-active", "ui-state-persist"}.issubset(set(a2.get("class").split())),
    }
    expected_3 = {
        "first_classes": ["ui-btn-active"],
        "second_classes": ["ui-btn-active", "ui-state-persist"],
        "persists_across_pages": True,
    }
    cases.append({
        "name": "navbar_active_and_persist_states",
        "summary": "Verify ui-btn-active and ui-state-persist class combinations for single- and multi-page navbars.",
        "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()
预期结果 · 待实际运行核验
  • 确认 3 项等宽 33.3% 与 6 项两列折行计算、home/arrow-r/search 图标与 4 种 data-iconpos 位置,以及 ui-btn-active + ui-state-persist 状态组合。
运行导航栏等宽/折行规则、data-icon/data-iconpos 与持久激活态离线核验

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

当前源码 SHA256:9e52f8a381eedb7c017145e3bee17bd78272b4c6acc23d0db107e4d8f3752294

下载本站示例源码

本地运行

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

下载jQuery Mobile 导航栏实验

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

ZIP SHA256:fde4615e8fc377bbdbb3f39f0dceec90038be9bd1abcea5af08a58ddfb9a319d

常见错误与边界

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

练习与解答

在多页 jQuery Mobile 应用的共享导航栏中,只添加 ui-btn-active 与同时添加 ui-btn-active ui-state-persist 有什么区别?

展开参考解答

仅添加 ui-btn-active 在页面往返切换时可能失去持久选中标记;叠加 ui-state-persist 后,框架在多页切换返回该视图时会持续保留当前标签的激活高亮状态。

规范与官方依据