jQuery Mobile 网格
梳理 jQuery Mobile 基于浮动类的等宽网格体系(ui-grid-solo 与 ui-grid-a..d 对应 1–5 列、ui-block-a..e 子块映射)、自定义 CSS 与内联样式、ui-block-a 自动换行规则以及 ui-responsive 窄屏堆叠响应。
学完这一章,你能够
- 梳理 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-solo | 1 | 100% | ui-block-a | grid_classes_and_block_mapping |
| ui-grid-a | 2 | 50% / 50% | ui-block-a | ui-block-b | grid_classes_and_block_mapping |
| ui-grid-b | 3 | 33.3% × 3 | ui-block-a | b | c | grid_classes_and_block_mapping |
| ui-grid-c | 4 | 25% × 4 | ui-block-a | b | c | d | grid_classes_and_block_mapping |
| ui-grid-d | 5 | 20% × 5 | ui-block-a | b | c | d | e | grid_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 离线核验
展开查看收起
"""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()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
会被排成 3 行,每行块数依次为 3、2、1,因为每出现一次 ui-block-a 都会清除浮动并强制开始新的一行。