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

jQuery Mobile 网格

梳理 jQuery Mobile 基于浮动类的等宽网格体系(ui-grid-solo 与 ui-grid-a..d 对应 1–5 列、ui-block-a..e 子块映射)、自定义 CSS 与内联样式、ui-block-a 自动换行规则以及 ui-responsive 窄屏堆叠响应。

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

学完这一章,你能够

网格布局体系与 1–5 列类名映射

由于手机竖屏宽度有限,常规长文本页面不建议过度分栏;但在需要把按钮、图标卡片或导航标签整齐并排时,jQuery Mobile 提供了纯 CSS 驱动的等宽浮动网格。网格各列总宽占 100%,默认不带边框、背景、外边距与内边距。除单列 `ui-grid-solo`(100%)外,框架提供四套多列网格类:`ui-grid-a`(2 列 50%)、`ui-grid-b`(3 列 33.3%)、`ui-grid-c`(4 列 25%)与 `ui-grid-d`(5 列 20%),容器内子块依次使用 `ui-block-a` 至 `ui-block-e` 对应各列。

自定义列块样式、多行折行与响应式堆叠

网格子块本身保持视觉中立,如需呈现卡片边框、固定高度、背景色或居中文字,可在样式表中统一选择器(如 `.ui-block-a, .ui-block-b, .ui-block-c { ... }`)定制,也可在特定子块上直接通过内联 `style` 调整样式。

同一网格容器支持排布多行内容:每当出现新的 `ui-block-a` 类时,浮动清除规则会强制开启新的一行(因此即使前一行未填满全部列数,后续 `ui-block-a` 也会另起一行)。在小屏设备上并排过多按钮容易导致文案截断,在网格容器上追加 `ui-responsive`(例如 `<div class="ui-grid-b ui-responsive">`)即可让各列在窄视口下自动堆叠为单列。在已停更的 jQuery Mobile 之外,现代布局建议直接使用原生 CSS Grid(`grid-template-columns: repeat(auto-fit, ...)`)与 Flexbox,无需维护固定字母类名。

jQuery Mobile 网格容器与子块类对照表

网格容器类列数等分列宽子元素类序列离线核验
ui-grid-solo1100%ui-block-agrid_classes_and_block_mapping
ui-grid-a250% / 50%ui-block-a | ui-block-bgrid_classes_and_block_mapping
ui-grid-b333.3% × 3ui-block-a | b | cgrid_classes_and_block_mapping
ui-grid-c425% × 4ui-block-a | b | c | dgrid_classes_and_block_mapping
ui-grid-d520% × 5ui-block-a | b | c | d | egrid_classes_and_block_mapping

网格子块匹配提示

主题配对规则
容器与 ui-block-* 严格对应两列网格 ui-grid-a 需配 ui-block-a 与 ui-block-b,三列网格 ui-grid-b 需配 ui-block-a/b/c,依此类推;各列通过左浮动并排。

运行 1–5 列网格映射、块样式定制、ui-block-a 换行与 ui-responsive 离线核验

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

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

GRID_SPECS = [
    ("ui-grid-solo", 1, ["ui-block-a"]),
    ("ui-grid-a", 2, ["ui-block-a", "ui-block-b"]),
    ("ui-grid-b", 3, ["ui-block-a", "ui-block-b", "ui-block-c"]),
    ("ui-grid-c", 4, ["ui-block-a", "ui-block-b", "ui-block-c", "ui-block-d"]),
    ("ui-grid-d", 5, ["ui-block-a", "ui-block-b", "ui-block-c", "ui-block-d", "ui-block-e"]),
]


def run_cases():
    cases = []

    # Case 1: All five grid container classes (ui-grid-solo, a, b, c, d) and equal column widths
    grid_summary = []
    for cls, cols, blocks in GRID_SPECS:
        children = "".join(f'<div class="{b}"><span>{b}</span></div>' for b in blocks)
        node = html.fragment_fromstring(f'<div class="{cls}">{children}</div>')
        parsed_blocks = [d.get("class") for d in node.xpath("./div")]
        grid_summary.append({
            "grid": node.get("class"),
            "cols": len(parsed_blocks),
            "col_width_pct": round(100.0 / cols, 2),
            "blocks_match": parsed_blocks == blocks,
        })
    actual_1 = {
        "grid_count": len(grid_summary),
        "all_blocks_match": all(item["blocks_match"] for item in grid_summary),
        "column_counts": [item["cols"] for item in grid_summary],
    }
    expected_1 = {
        "grid_count": 5,
        "all_blocks_match": True,
        "column_counts": [1, 2, 3, 4, 5],
    }
    cases.append({
        "name": "grid_classes_and_block_mapping",
        "summary": "Verify ui-grid-solo through ui-grid-d column counts (1..5) and matching ui-block-a..e children.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Custom CSS styling and inline block styling
    doc2 = html.fragment_fromstring(
        """
        <div>
          <style>
            .ui-block-a, .ui-block-b, .ui-block-c {
              background-color: lightgray;
              border: 1px solid black;
              height: 100px;
              font-weight: bold;
              text-align: center;
              padding: 30px;
            }
          </style>
          <div class="ui-block-a" style="border: 1px solid black;"><span>Text..</span></div>
        </div>
        """
    )
    css_text = doc2.xpath("string(./style)")
    actual_2 = {
        "styles_three_blocks": all(sel in css_text for sel in (".ui-block-a", ".ui-block-b", ".ui-block-c")),
        "has_height_100px": bool(re.search(r"height\s*:\s*100px", css_text)),
        "inline_border": "border: 1px solid black" in doc2.xpath("./div/@style")[0],
    }
    expected_2 = {
        "styles_three_blocks": True,
        "has_height_100px": True,
        "inline_border": True,
    }
    cases.append({
        "name": "grid_custom_css_and_inline",
        "summary": "Verify stylesheet and inline style customization on ui-block-a/b/c grid cells.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Multi-row grid segmentation (every ui-block-a starts a new row) + ui-responsive modifier
    doc3 = html.fragment_fromstring(
        """
        <div class="ui-grid-b ui-responsive">
          <div class="ui-block-a"><span>R1C1</span></div>
          <div class="ui-block-b"><span>R1C2</span></div>
          <div class="ui-block-c"><span>R1C3</span></div>
          <div class="ui-block-a"><span>R2C1</span></div>
          <div class="ui-block-b"><span>R2C2</span></div>
          <div class="ui-block-a"><span>R3C1</span></div>
        </div>
        """
    )
    rows = []
    for block in doc3.xpath("./div"):
        cls = block.get("class")
        if cls == "ui-block-a" or not rows:
            rows.append([])
        rows[-1].append(cls)
    actual_3 = {
        "is_responsive": "ui-responsive" in doc3.get("class", "").split(),
        "row_count": len(rows),
        "row_lengths": [len(r) for r in rows],
    }
    expected_3 = {
        "is_responsive": True,
        "row_count": 3,
        "row_lengths": [3, 2, 1],
    }
    cases.append({
        "name": "grid_multirow_and_responsive",
        "summary": "Verify ui-block-a new-row breaking semantics across 3 rows and ui-responsive stacking modifier.",
        "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()
预期结果 · 待实际运行核验
  • 确认 ui-grid-solo..d 与 ui-block-a..e 完整映射、样式表与内联边框规则,以及三行(3/2/1 块)换行切分与 ui-responsive 修饰类。
运行 1–5 列网格映射、块样式定制、ui-block-a 换行与 ui-responsive 离线核验

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

当前源码 SHA256:87efe3ef0d46ea8b6fab5f8cfb2d72669527e547970b2efc16120ad07efbade2

下载本站示例源码

本地运行

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

下载jQuery Mobile 网格实验

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

ZIP SHA256:ca571c88db65fae0a91c03cbe75867c9e2364b2a914dfaed8c277c9c888bc3f3

常见错误与边界

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

练习与解答

在 ui-grid-b 三列网格中依次放入 ui-block-a、ui-block-b、ui-block-c、ui-block-a、ui-block-b、ui-block-a 六个子块,它们会被排成几行、每行有几个块?

展开参考解答

会被排成 3 行,每行块数依次为 3、2、1,因为每出现一次 ui-block-a 都会清除浮动并强制开始新的一行。

规范与官方依据