jQuery Mobile 按钮
系统梳理 jQuery Mobile 创建按钮的三种 HTML 元素、导航/内联/组合/后退按钮用法,以及 1.4 版本 ui-btn 样式修饰类(ui-btn-b、ui-corner-all、ui-mini、ui-shadow)的组合规律与语义选型建议。
学完这一章,你能够
- 系统梳理 jQuery Mobile 创建按钮的三种 HTML 元素、导航/内联/组合/后退按钮用法,以及 1.4 版本 ui-btn 样式修饰类(ui-btn-b、ui-corner-all、ui-mini、ui-shadow)的组合规律与语义选型建议。
三种按钮创建方式与语义分工
触屏移动应用高度依赖清晰易点的按钮控件。在 jQuery Mobile 中有三种方式生成按钮:使用 `<button>` 元素、使用 `<input type="button|submit|reset">` 表单控件,或为 `<a>` 链接添加 `data-role="button"`(在 1.4 版本中官方推荐直接为 `<a>` 或 `<button>` 添加 `class="ui-btn"` 以减少运行时 DOM 改写)。一般约定用链接按钮做页面导航,用 `<button>` 或 `<input>` 承接表单提交与交互动作。
导航、内联、组合与后退按钮
页面间导航只需给指向目标锚点(如 `href="#pagetwo"`)的 `<a>` 赋予按钮样式。默认情况下按钮会占满父容器整行宽度;若希望按钮仅包裹文本宽度或将多个按钮并排摆放,可添加 `data-inline="true"`(1.4 等价类为 `ui-btn-inline`)。
需要将一组相关操作紧贴成整体时,可用 `<div data-role="controlgroup" data-type="horizontal|vertical">` 包裹按钮:缺省为垂直叠放,相邻按钮间无外边距且仅首尾元素保留圆角。若要实现返回上一浏览记录的后退按钮,在链接上添加 `data-rel="back"` 即可,框架会优先执行历史回退并忽略 `href` 目标。
1.4 样式修饰类与现代 CSS 对照
除基础 `ui-btn` 外,可通过空格分隔叠加多种视觉修饰类,例如深色主题 `ui-btn-b`、圆角 `ui-corner-all`、紧凑尺寸 `ui-mini` 与阴影 `ui-shadow`。需留意默认外观差异:`<input>` 按钮由增强小部件自动附带圆角与阴影,而仅加 `ui-btn` 的 `<a>` 与 `<button>` 默认不带圆角和阴影,需显式叠加类名(另注:1.4 推荐 `ui-corner-all` 而非已废弃的 `ui-btn-corner-all`)。现代项目因 jQuery Mobile 停更,可直接使用原生 `<button>` 搭配 CSS Flexbox 与设计令牌样式表实现同等效果;下一章将展开按钮图标配置。
按钮元素选型建议
| 主题 | 语义分工说明 |
|---|---|
| 链接与表单按钮分工 | 页面跳转优先采用 <a> 链接按钮(data-role="button" 或 class="ui-btn");表单提交与本地操作优先采用 <button> 或 <input>,兼顾可访问性与语义规范。 |
组合按钮默认行为提示
| 主题 | 默认排版特征 |
|---|---|
| controlgroup 默认垂直堆叠 | 未指定 data-type 时默认垂直组合,按钮紧贴无间隙,仅首个与最后一个按钮保留外侧圆角。 |
常用按钮 CSS 修饰类核对表
| CSS 类 | 视觉效果 | 离线核验场景 |
|---|---|---|
| ui-btn-b | 切换为深色(黑色背景、白色文字)主题按钮,覆盖默认浅灰黑字样式。 | ui_btn_modifier_classes 核验 |
| ui-corner-all | 为按钮四周应用统一圆角边框。 | ui_btn_modifier_classes 核验 |
| ui-mini | 缩小字号与内边距,渲染紧凑型迷你按钮。 | ui_btn_modifier_classes 核验 |
| ui-shadow | 为按钮容器叠加柔和投影效果。 | ui_btn_modifier_classes 核验 |
多类叠加与元素默认样式差异
| 主题 | 注意事项 |
|---|---|
| 空格分隔叠加类名 | 多个修饰类以空格拼接(如 class="ui-btn ui-btn-inline ui-corner-all ui-shadow");<input> 按钮默认带圆角与阴影,而 <a> 和 <button> 需按需显式声明。 |
运行三类按钮、内联/后退属性、controlgroup 组合与 ui-btn 修饰类离线核验
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 06: buttons."""
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: Three ways to create buttons (<button>, <input>, <a data-role="button">)
doc1 = html.fragment_fromstring(
"""
<div data-role="main" class="ui-content">
<button>按钮</button>
<input type="button" value="按钮">
<a href="#" data-role="button">按钮</a>
</div>
"""
)
actual_1 = {
"button_tags": [el.tag for el in doc1.xpath("./*")],
"input_type": doc1.xpath("./input/@type")[0],
"anchor_role": doc1.xpath("./a/@data-role")[0],
}
expected_1 = {
"button_tags": ["button", "input", "a"],
"input_type": "button",
"anchor_role": "button",
}
cases.append({
"name": "three_button_creation_patterns",
"summary": "Verify <button>, <input type='button'>, and <a data-role='button'> creation patterns.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Navigation button, inline button, and back button
doc2 = html.fragment_fromstring(
"""
<div>
<a href="#pagetwo" data-role="button">访问第二个页面</a>
<a href="#pagetwo" data-role="button" data-inline="true">内联导航按钮</a>
<a href="#" data-role="button" data-rel="back">返回</a>
</div>
"""
)
nav_btn, inline_btn, back_btn = doc2.xpath("./a")
actual_2 = {
"nav_href": nav_btn.get("href"),
"inline_flag": inline_btn.get("data-inline") == "true",
"back_rel": back_btn.get("data-rel"),
}
expected_2 = {
"nav_href": "#pagetwo",
"inline_flag": True,
"back_rel": "back",
}
cases.append({
"name": "nav_inline_and_back_buttons",
"summary": "Verify page navigation href, data-inline='true' width shrink, and data-rel='back' history return.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Controlgroup horizontal vs default vertical grouping
doc3 = html.fragment_fromstring(
"""
<div>
<div data-role="controlgroup" data-type="horizontal">
<a href="#anylink" data-role="button">按钮 1</a>
<a href="#anylink" data-role="button">按钮 2</a>
<a href="#anylink" data-role="button">按钮 3</a>
</div>
<div data-role="controlgroup">
<a href="#anylink" data-role="button">默认垂直 1</a>
<a href="#anylink" data-role="button">默认垂直 2</a>
</div>
</div>
"""
)
h_grp, v_grp = doc3.xpath("./div[@data-role='controlgroup']")
actual_3 = {
"horizontal_type": h_grp.get("data-type"),
"horizontal_count": len(h_grp.xpath("./a[@data-role='button']")),
"default_vertical_type": v_grp.get("data-type", "vertical"),
"vertical_count": len(v_grp.xpath("./a[@data-role='button']")),
}
expected_3 = {
"horizontal_type": "horizontal",
"horizontal_count": 3,
"default_vertical_type": "vertical",
"vertical_count": 2,
}
cases.append({
"name": "controlgroup_button_grouping",
"summary": "Verify data-role='controlgroup' horizontal layout and default vertical fallback.",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: jQM 1.4 ui-btn CSS modifier classes
doc4 = html.fragment_fromstring(
"""
<div>
<a href="#" class="ui-btn ui-btn-b">深色主题</a>
<a href="#" class="ui-btn ui-corner-all">圆角按钮</a>
<a href="#" class="ui-btn ui-mini">迷你按钮</a>
<a href="#" class="ui-btn ui-shadow">阴影按钮</a>
<a href="#" class="ui-btn ui-btn-inline ui-corner-all ui-shadow">组合类按钮</a>
</div>
"""
)
modifier_classes = [a.get("class").split()[1] for a in doc4.xpath("./a")[:4]]
combo_classes = doc4.xpath("./a")[4].get("class").split()
actual_4 = {
"modifier_classes": modifier_classes,
"combo_has_all_four": set(combo_classes) == {"ui-btn", "ui-btn-inline", "ui-corner-all", "ui-shadow"},
}
expected_4 = {
"modifier_classes": ["ui-btn-b", "ui-corner-all", "ui-mini", "ui-shadow"],
"combo_has_all_four": True,
}
cases.append({
"name": "ui_btn_modifier_classes",
"summary": "Verify ui-btn-b, ui-corner-all, ui-mini, ui-shadow, and multi-class composition.",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
return cases
def main():
print(json.dumps({"version": VERSION, "cases": run_cases()}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 确认 button/input/a 三种创建方式、nav/inline/back 属性、水平与默认垂直 controlgroup,以及 ui-btn-b/ui-corner-all/ui-mini/ui-shadow 四类叠加解析。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:3ce84d9fe99d4d90fb9b89a12fb82ba457b4343c86da0e84a4a3e2f660fd48ea
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:a3f8d9267e6cca7a5638a787e6f32bee4f92ad12d01dccb82e9189c8d2deb394
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在 jQuery Mobile 中使用 data-rel="back" 创建返回按钮时,按钮上的 href 属性会起什么作用?
展开参考解答
在支持历史记录的正常环境下,jQuery Mobile 会拦截点击并直接执行浏览器历史后退,忽略 href 指向;仅当不支持脚本或无法回退时,href 才可作为静态降级链接。