jQuery Mobile 表单滑动条
系统梳理 jQuery Mobile 的数值滑动条(input[type='range'] 的 min/max/step/value、data-show-value、data-popup-enabled、data-highlight)与拨动开关(checkbox + data-role='flipswitch'、自定义开关文案、checked 预选中),并补充双端区间滑块与现代原生替代方案。
学完这一章,你能够
- 系统梳理 jQuery Mobile 的数值滑动条(input[type='range'] 的 min/max/step/value、data-show-value、data-popup-enabled、data-highlight)与拨动开关(checkbox + data-role='flipswitch'、自定义开关文案、checked 预选中),并补充双端区间滑块与现代原生替代方案。
数值滑动条控件与增强属性
滑动条允许用户通过拖动滑块从连续或离散数值区间中选取数字,核心 HTML 标签为标准 `<input type="range">`。常用数值约束由四项标准属性控制:`min` 规定允许的最小值、`max` 规定允许的最大值、`step` 规定合法数字步进间隔、`value` 规定初始默认值。现代浏览器已广泛原生支持 `<input type="range">` 并可通过 CSS `accent-color` 及伪元素定制轨道与滑块,无需依赖已于 2021 年归档的 jQuery Mobile 脚本。
jQuery Mobile 在标准范围输入框之上提供了三项实用的视觉增强属性:添加 `data-show-value="true"` 可在滑块手柄内部直接显示当前数值;添加 `data-popup-enabled="true"` 会在拖动时于手柄上方弹出类似气泡提示框的实时读数;添加 `data-highlight="true"` 则会高亮填充从起点到当前值之间的左侧轨道区间。
拨动开关(Flipswitch)与区间滑块扩展
拨动开关专门用于二元状态切换(如开/关、是/否)。在 jQuery Mobile 1.4 中,推荐使用 `<input type="checkbox" data-role="flipswitch">` 创建开关(早期版本则使用包含两个 `<option>` 的 `<select data-role="slider">`)。开关默认文案为 `On` 和 `Off`,可通过 `data-on-text` 与 `data-off-text` 自定义(例如改为 `True`/`False`);若希望初始处于开启状态,直接添加标准 HTML `checked` 属性即可。在现代 Web 开发中,语义化拨动开关通常采用 `<input type="checkbox" role="switch">` 配合纯 CSS 过渡动画实现。
进阶场景下,还可将两个 `<input type="range">` 放入 `<div data-role="rangeslider">` 容器制作同时限定上下界的区间滑块,或通过覆盖 `.ui-flipswitch` 相关 CSS 规则自定义开关的轨道尺寸、圆角与背景配色。
运行 jQuery Mobile 滑动条边界步进、高亮提示与拨动开关离线验证
展开查看收起
"""Offline jQuery Mobile Form Sliders & Flipswitch verifier (Chapter 21)."""
from __future__ import annotations
import json
from lxml import html
def clamp_step(val: float, lo: float, hi: float, step: float = 1.0) -> float:
clamped = max(lo, min(hi, val))
steps = round((clamped - lo) / step)
return round(lo + steps * step, 6)
def main() -> None:
cases = []
# Case 1: range_slider_bounds_and_step
markup1 = """
<form method="post" action="demoform.php">
<label for="points">进度:</label>
<input type="range" name="points" id="points" value="50" min="0" max="100" step="5">
</form>
"""
doc1 = html.fragment_fromstring(markup1, create_parent=False)
inp = doc1.xpath(".//input")[0]
lo, hi, step, val = float(inp.get("min")), float(inp.get("max")), float(inp.get("step")), float(inp.get("value"))
actual1 = {
"type": inp.get("type"),
"default_value": val,
"clamped_high": clamp_step(130, lo, hi, step),
"clamped_low": clamp_step(-10, lo, hi, step),
"quantized_43": clamp_step(43, lo, hi, step),
}
expected1 = {
"type": "range",
"default_value": 50.0,
"clamped_high": 100.0,
"clamped_low": 0.0,
"quantized_43": 45.0,
}
cases.append({
"name": "range_slider_bounds_and_step",
"summary": "解析 input[type='range'] 的 min/max/step/value 限制并验证越界截断与步进量化。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: slider_value_popup_highlight
markup2 = """
<div>
<input id="s1" type="range" data-show-value="true">
<input id="s2" type="range" data-popup-enabled="true">
<input id="s3" type="range" data-highlight="true">
</div>
"""
doc2 = html.fragment_fromstring(markup2, create_parent=False)
actual2 = {
"show_value": doc2.xpath("string(.//input[@id='s1']/@data-show-value)"),
"popup_enabled": doc2.xpath("string(.//input[@id='s2']/@data-popup-enabled)"),
"highlight": doc2.xpath("string(.//input[@id='s3']/@data-highlight)"),
}
expected2 = {"show_value": "true", "popup_enabled": "true", "highlight": "true"}
cases.append({
"name": "slider_value_popup_highlight",
"summary": "验证滑块 data-show-value、data-popup-enabled 与 data-highlight 三个增强属性。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: flipswitch_checkbox_basic
markup3 = """
<form method="post" action="demoform.php">
<label for="switch">切换开关:</label>
<input type="checkbox" data-role="flipswitch" name="switch" id="switch">
</form>
"""
doc3 = html.fragment_fromstring(markup3, create_parent=False)
sw = doc3.xpath(".//input")[0]
on_text = sw.get("data-on-text", "On")
off_text = sw.get("data-off-text", "Off")
checked = "checked" in sw.attrib
actual3 = {
"data_role": sw.get("data-role"),
"default_checked": checked,
"default_labels": [on_text, off_text],
"rendered_label": on_text if checked else off_text,
}
expected3 = {
"data_role": "flipswitch",
"default_checked": False,
"default_labels": ["On", "Off"],
"rendered_label": "Off",
}
cases.append({
"name": "flipswitch_checkbox_basic",
"summary": "验证 checkbox + data-role='flipswitch' 拨动开关的默认 On/Off 文本与未选中初态。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: flipswitch_custom_text_and_checked
markup4 = """
<input type="checkbox" data-role="flipswitch" name="switch" id="switch"
data-on-text="True" data-off-text="False" checked>
"""
sw4 = html.fragment_fromstring(markup4, create_parent=False)
checked4 = "checked" in sw4.attrib
actual4 = {
"on_text": sw4.get("data-on-text"),
"off_text": sw4.get("data-off-text"),
"checked": checked4,
"active_label": sw4.get("data-on-text") if checked4 else sw4.get("data-off-text"),
}
expected4 = {
"on_text": "True",
"off_text": "False",
"checked": True,
"active_label": "True",
}
cases.append({
"name": "flipswitch_custom_text_and_checked",
"summary": "验证 data-on-text/data-off-text 自定义文本与 checked 预激活状态。",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
# Case 5: rangeslider_dual_and_legacy_select
markup5 = """
<div>
<div data-role="rangeslider">
<input type="range" id="range-low" min="0" max="100" value="20">
<input type="range" id="range-high" min="0" max="100" value="80">
</div>
<select id="legacy_flip" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
"""
doc5 = html.fragment_fromstring(markup5, create_parent=False)
low = int(doc5.xpath("string(.//input[@id='range-low']/@value)"))
high = int(doc5.xpath("string(.//input[@id='range-high']/@value)"))
actual5 = {
"dual_range_valid": 0 <= low <= high <= 100,
"span": high - low,
"legacy_slider_options": doc5.xpath(".//select[@data-role='slider']/option/@value"),
}
expected5 = {
"dual_range_valid": True,
"span": 60,
"legacy_slider_options": ["off", "on"],
}
cases.append({
"name": "rangeslider_dual_and_legacy_select",
"summary": "验证双滑块区间控件 rangeslider 及早期 select[data-role='slider'] 二选一开关对照。",
"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 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
在 <input type="checkbox" data-role="flipswitch"> 上添加 data-on-text="开" data-off-text="关" checked 属性即可。