jQuery Mobile 主题
系统梳理 jQuery Mobile 1.4 的双内置主题样本(a 浅灰、b 深黑)、按钮 ui-btn-a|b 类、覆盖 14 类组件(头尾栏/对话框/按钮/图标/弹窗/导航栏/面板/折叠块/列表/分割按钮/表单等)的 data-theme 继承与局部覆写规则、自定义主题 c–z CSS 类写法及 1.4 纯 CSS 主题继承演进。
学完这一章,你能够
- 系统梳理 jQuery Mobile 1.4 的双内置主题样本(a 浅灰、b 深黑)、按钮 ui-btn-a|b 类、覆盖 14 类组件(头尾栏/对话框/按钮/图标/弹窗/导航栏/面板/折叠块/列表/分割按钮/表单等)的 data-theme 继承与局部覆写规则、自定义主题 c–z CSS 类写法及 1.4 纯 CSS 主题继承演进。
双内置主题样本 a / b 与按钮类
jQuery Mobile 1.4 精简并提供了 2 套默认主题样本(swatch):`a`(默认浅色,灰底黑字)与 `b`(深色,黑底白字、木炭色按钮),两者激活状态的按钮和链接均采用蓝底白字高亮。除通过 `data-theme="a|b"` 为各类容器设置主题外,对使用 `class="ui-btn"` 的链接或按钮可直接附加 `ui-btn-a` 或 `ui-btn-b` 切换灰色或黑色外观。大多数元素默认采用 `a` 主题,且子元素通常自动继承父容器主题。在现代前端开发中,这类配色体系已被 CSS 自定义属性(`--color-bg` 等)与 `prefers-color-scheme` 暗色模式媒体查询取代。
工具栏、对话框、弹窗、面板与按钮图标主题
为具体页面区块定制主题时,只需在对应元素上设置 `data-theme="b"` 或 `ui-btn-b`:包括头部栏与尾部栏(`data-role="header"` / `footer`)、对话框页面(`data-role="page" data-dialog="true"`)的头尾栏、独立按钮(`class="ui-btn ui-btn-b"`)、仅图标按钮(`class="ui-btn ui-btn-b ui-icon-search ui-btn-icon-notext"`)、弹窗(`data-role="popup"`)、侧滑面板(`data-role="panel"`),以及在默认浅色头尾栏中穿插单个黑色 `ui-btn-b` 按钮形成视觉对比。
导航栏、可折叠块、列表与表单主题继承
在复合组件中,父级主题与子项局部覆写可灵活配合:导航栏本身从父级工具栏继承主题,但可为内部特定 `<a>` 单独指定 `data-theme="a"` 反向突出;可折叠块、可折叠列表及可折叠表单(`<fieldset data-role="collapsible">`)通过 `data-theme` 控制标题栏配色、通过 `data-content-theme` 控制展开内容区配色;列表视图(`data-role="listview"`)支持在 `<ul>` 设 `data-theme="b"` 并在单个 `<li>` 上用 `data-theme="a"` 局部覆写,分割按钮右侧图标则由 `data-split-theme="b"` 独立着色;表单内的 `<input>` 与 `<select>` 同样支持直接设置 `data-theme="b"`。
自定义主题(c–z)与 1.4 纯 CSS 继承演进
若内置 `a`/`b` 无法满足品牌配色,开发者可复制并重命名字母后缀为 `c` 到 `z` 的样式模块来扩展新主题,或在页面 `<style>` 中定义对应主题类:工具栏对应 `.ui-bar-(a-z)`、内容区对应 `.ui-body-(a-z)`、页面容器对应 `.ui-page-theme-(a-z)`(如 `.ui-bar-f`、`.ui-body-f`、`.ui-page-theme-f`)。值得注意的是,jQuery Mobile 1.4 之前依赖 JavaScript 遍历 DOM 计算父主题继承,1.4 起出于性能考量已全面改为纯 CSS 级联继承,早期官方曾提供 ThemeRoller 工具辅助升级旧主题文件。
jQuery Mobile 1.4 内置主题样本 a / b 对照表
| 主题值 | 配色与控件表现 | 对应验证 |
| a | 默认浅色主题:页面、头部、底部与按钮均为灰色背景配黑色文字;激活态按钮和链接为蓝底白字;输入框 placeholder 为浅灰色、输入值 value 为黑色。 | 内置浅色样本核对 |
| b | 深色主题:页面、头部与底部为黑色背景配白色文字;按钮为木炭色背景配白色文字;激活态同为蓝底白字;输入框 placeholder 为浅灰色、value 为白色。 | 内置深色样本核对 |
默认主题与继承提示
| 主题 | 规则说明 |
| 默认主题 a 与子元素继承 | 大多数元素默认应用主题 a,未显式声明 data-theme 的子元素会自动继承父容器主题样本。 |
1.4 版本主题继承性能改进说明
| 主题 | 规则说明 |
| 纯 CSS 继承与 ThemeRoller 迁移 | jQuery Mobile 1.4 起放弃 JavaScript 运行时继承计算,改为纯 CSS 级联继承主题;旧版本自定义主题需借助 ThemeRoller 升级兼容。 |
运行 jQuery Mobile 主题样本 a/b、继承覆盖与自定义主题 f 离线验证
展开查看收起
"""Offline jQuery Mobile Themes swatch & CSS inheritance verifier (Chapter 22)."""
from __future__ import annotations
import json
import re
from lxml import html
def resolve_effective_theme(el, default: str = "a") -> str:
cur = el
while cur is not None:
theme = cur.get("data-theme")
if theme:
return theme
cur = cur.getparent()
return default
def main() -> None:
cases = []
# Case 1: theme_swatches_a_and_b
swatches = {
"a": {"page_bg": "gray", "text": "black", "btn_bg": "gray", "active_bg": "blue", "active_text": "white"},
"b": {"page_bg": "black", "text": "white", "btn_bg": "charcoal", "active_bg": "blue", "active_text": "white"},
}
btns = html.fragment_fromstring(
'<div><a href="#" class="ui-btn ui-btn-a">浅色</a><a href="#" class="ui-btn ui-btn-b">深色</a></div>',
create_parent=False,
)
actual1 = {
"builtin_swatches": sorted(swatches.keys()),
"btn_classes": btns.xpath("./a/@class"),
"active_consistent": swatches["a"]["active_bg"] == swatches["b"]["active_bg"] == "blue",
}
expected1 = {
"builtin_swatches": ["a", "b"],
"btn_classes": ["ui-btn ui-btn-a", "ui-btn ui-btn-b"],
"active_consistent": True,
}
cases.append({
"name": "theme_swatches_a_and_b",
"summary": "核对内置主题样本 a(灰底黑字)与 b(黑底白字)及 ui-btn-a/ui-btn-b 按钮类配置。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: header_footer_dialog_popup_panel_themes
markup2 = """
<div data-role="page" data-dialog="true" id="pagetwo">
<div data-role="header" data-theme="b">
<a href="#" class="ui-btn ui-btn-b">主页</a>
<a href="#" class="ui-btn ui-btn-b ui-icon-search ui-btn-icon-notext">Search</a>
</div>
<div data-role="panel" id="myPanel" data-theme="b"></div>
<div data-role="popup" id="myPopup" data-theme="b"></div>
<div data-role="footer" data-theme="b"></div>
</div>
"""
doc2 = html.fragment_fromstring(markup2, create_parent=False)
actual2 = {
"dialog_flag": doc2.get("data-dialog"),
"themed_b_roles": doc2.xpath(".//*[@data-theme='b']/@data-role"),
"icon_only_btn": "ui-btn-icon-notext" in doc2.xpath("string(.//a[2]/@class)"),
}
expected2 = {
"dialog_flag": "true",
"themed_b_roles": ["header", "panel", "popup", "footer"],
"icon_only_btn": True,
}
cases.append({
"name": "header_footer_dialog_popup_panel_themes",
"summary": "验证头部栏、尾部栏、对话框、面板、弹窗与图标按钮的主题 b 标记解析。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: navbar_and_listview_theme_inheritance
markup3 = """
<div data-role="page">
<div data-role="footer" data-theme="b">
<div data-role="navbar">
<ul>
<li><a id="nb1" href="#" data-icon="home" data-theme="a">Button 1</a></li>
<li><a id="nb2" href="#" data-icon="arrow-r">Button 2</a></li>
</ul>
</div>
</div>
<div data-role="collapsible" data-theme="b" data-content-theme="b">
<ul data-role="listview" data-theme="b" data-split-theme="b">
<li id="li1"><a href="#">Item 1</a></li>
<li id="li2" data-theme="a"><a href="#">Item 2</a></li>
</ul>
</div>
</div>
"""
doc3 = html.fragment_fromstring(markup3, create_parent=False)
actual3 = {
"nb1_override": resolve_effective_theme(doc3.xpath(".//a[@id='nb1']")[0]),
"nb2_inherited": resolve_effective_theme(doc3.xpath(".//a[@id='nb2']")[0]),
"li1_inherited": resolve_effective_theme(doc3.xpath(".//li[@id='li1']")[0]),
"li2_override": resolve_effective_theme(doc3.xpath(".//li[@id='li2']")[0]),
"split_theme": doc3.xpath("string(.//ul[@data-role='listview']/@data-split-theme)"),
"content_theme": doc3.xpath("string(.//div[@data-role='collapsible']/@data-content-theme)"),
}
expected3 = {
"nb1_override": "a",
"nb2_inherited": "b",
"li1_inherited": "b",
"li2_override": "a",
"split_theme": "b",
"content_theme": "b",
}
cases.append({
"name": "navbar_and_listview_theme_inheritance",
"summary": "验证导航栏、可折叠块、列表视图与分割按钮的父级主题继承及子项局部覆盖规则。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: themed_forms_and_collapsible_fieldset
markup4 = """
<form method="post" action="demo.php">
<fieldset data-role="collapsible" data-theme="b" data-content-theme="b">
<legend>Click me - I'm collapsible!</legend>
<input type="text" id="name" data-theme="b">
<select id="colors" data-theme="b"><option value="red">Red</option></select>
</fieldset>
</form>
"""
doc4 = html.fragment_fromstring(markup4, create_parent=False)
actual4 = {
"fieldset_role": doc4.xpath("string(.//fieldset/@data-role)"),
"input_theme": doc4.xpath("string(.//input/@data-theme)"),
"select_theme": doc4.xpath("string(.//select/@data-theme)"),
"fieldset_content_theme": doc4.xpath("string(.//fieldset/@data-content-theme)"),
}
expected4 = {
"fieldset_role": "collapsible",
"input_theme": "b",
"select_theme": "b",
"fieldset_content_theme": "b",
}
cases.append({
"name": "themed_forms_and_collapsible_fieldset",
"summary": "验证表单输入框、选择菜单与可折叠表单 fieldset 的主题属性配置。",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
# Case 5: custom_swatch_css_rules
css_snippet = """
.ui-bar-f { color: red; background-color: yellow; }
.ui-body-f { font-weight: bold; color: white; background-color: purple; }
.ui-page-theme-f { font-weight: bold; background-color: green; }
"""
selectors = re.findall(r"\.(ui-(?:bar|body|page-theme)-[a-z])\b", css_snippet)
actual5 = {
"custom_swatch_selectors": selectors,
"valid_custom_letter_range": all(s[-1] in "cdefghijklmnopqrstuvwxyz" for s in selectors),
"v14_inheritance_mechanism": "pure_css",
}
expected5 = {
"custom_swatch_selectors": ["ui-bar-f", "ui-body-f", "ui-page-theme-f"],
"valid_custom_letter_range": True,
"v14_inheritance_mechanism": "pure_css",
}
cases.append({
"name": "custom_swatch_css_rules",
"summary": "验证自定义主题 f 的 ui-bar-f/ui-body-f/ui-page-theme-f 选择器及 1.4 纯 CSS 继承机制。",
"expected": expected5,
"actual": actual5,
"passed": actual5 == expected5,
})
print(json.dumps({
"version": "CPython 3.12.14 / lxml offline jQuery Mobile DOM & reference verifier",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
纯 CSS 级联继承可显著减少 DOM 遍历与脚本增强开销、提升移动端渲染性能;自定义主题可使用 c 到 z 字母后缀,并定义 .ui-bar-(a-z)、.ui-body-(a-z) 与 .ui-page-theme-(a-z) 等对应类。