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

Ionic Checkbox 复选框

解析 Ionic 1 Checkbox 复选框在列表中的标准嵌套写法(li.item.item-checkbox > label.checkbox > input[type=checkbox])、主题配色类(checkbox-*)以及多选状态独立性与现代 ion-checkbox 演进。

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

学完这一章,你能够

Checkbox 列表结构与主题色定制

Ionic 1 中的 Checkbox(复选框)底层依然基于标准 `<input type="checkbox">`,交互逻辑与原生复选框一致,但通过移动端风格样式统一了不同浏览器的勾选外观并扩大了可触控热区。构建多选列表时,外层使用 `<ul class="list">`,每个选项必须在 `.item` 后添加 `item-checkbox` 类,内部再用 `<label class="checkbox">` 包裹 `<input type="checkbox">` 并紧跟选项文字;如需定制选中高亮色,可在 `label.checkbox` 上叠加 `checkbox-assertive`、`checkbox-positive` 等九套主题配色类。源页面的示例按钮与内嵌 `iframe` 仅做静态登记不联网请求;现代 Ionic 7/8 已升级为 `<ion-checkbox color="danger">` 并原生支持 `indeterminate` 半选态。

运行 Ionic 1 Checkbox 列表 DOM、配色主题与多选状态机离线核验

python · 阅读源码
"""Offline Ionic 1 Checkbox list DOM, color themes & multi-select state verifier for Ch10 (checkbox)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"
THEMES = ["light", "stable", "positive", "calm", "balanced", "energized", "assertive", "royal", "dark"]


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_checkbox_list_dom() -> dict:
    markup = """
    <ul class="list">
      <li class="item item-checkbox">
        <label class="checkbox">
          <input type="checkbox" checked="checked">
        </label>
        Flux Capacitor
      </li>
      <li class="item item-checkbox">
        <label class="checkbox checkbox-assertive">
          <input type="checkbox">
        </label>
        1.21 Gigawatts
      </li>
    </ul>
    """
    root = html.fragment_fromstring(markup)
    rows = []
    for li in root.xpath("./li"):
        lbl = li.xpath("./label")[0]
        inp = lbl.xpath("./input")[0]
        text = "".join(t.strip() for t in li.xpath("./text()") if t.strip())
        rows.append({
            "has_item_checkbox": "item-checkbox" in li.get("class", "").split(),
            "label_classes": lbl.get("class", "").split(),
            "input_type": inp.get("type"),
            "checked": inp.get("checked") is not None,
            "text": text,
        })
    actual = rows
    expected = [
        {
            "has_item_checkbox": True,
            "label_classes": ["checkbox"],
            "input_type": "checkbox",
            "checked": True,
            "text": "Flux Capacitor",
        },
        {
            "has_item_checkbox": True,
            "label_classes": ["checkbox", "checkbox-assertive"],
            "input_type": "checkbox",
            "checked": False,
            "text": "1.21 Gigawatts",
        },
    ]
    return make_case(
        "checkbox_item_list_markup",
        "Verify ul.list > li.item.item-checkbox > label.checkbox > input[type=checkbox] markup",
        expected,
        actual,
    )


def case_checkbox_theme_and_multiselect() -> dict:
    options = [
        {"id": "flux", "label": "Flux Capacitor", "theme": "checkbox-positive", "checked": True},
        {"id": "power", "label": "1.21 Gigawatts", "theme": "checkbox-assertive", "checked": False},
        {"id": "speed", "label": "88 MPH", "theme": "checkbox-balanced", "checked": True},
    ]
    # Toggle second item on and first item off
    options[1]["checked"] = True
    options[0]["checked"] = False
    selected_ids = [o["id"] for o in options if o["checked"]]
    valid_themes = all(o["theme"] in {f"checkbox-{t}" for t in THEMES} for o in options)
    actual = {
        "selected_ids": selected_ids,
        "selected_count": len(selected_ids),
        "all_themes_valid": valid_themes,
        "theme_palette_size": len(THEMES),
    }
    expected = {
        "selected_ids": ["power", "speed"],
        "selected_count": 2,
        "all_themes_valid": True,
        "theme_palette_size": 9,
    }
    return make_case(
        "checkbox_theme_classes_and_multiselect_state",
        "Verify multi-select independence and checkbox-* theme class palette",
        expected,
        actual,
    )


def case_modern_ion_checkbox_mapping() -> dict:
    actual = {
        "v1_wrapper": "li.item.item-checkbox > label.checkbox > input[type=checkbox]",
        "v7_v8_component": "<ion-item><ion-checkbox color=\"danger\">...</ion-checkbox></ion-item>",
        "supports_indeterminate_in_modern": True,
    }
    expected = {
        "v1_wrapper": "li.item.item-checkbox > label.checkbox > input[type=checkbox]",
        "v7_v8_component": "<ion-item><ion-checkbox color=\"danger\">...</ion-checkbox></ion-item>",
        "supports_indeterminate_in_modern": True,
    }
    return make_case(
        "checkbox_v1_to_modern_mapping",
        "Verify Ionic 1 CSS checkbox to Ionic 7/8 <ion-checkbox> Web Component equivalence",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_checkbox_list_dom(),
        case_checkbox_theme_and_multiselect(),
        case_modern_ion_checkbox_mapping(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 验证 `li.item.item-checkbox`、`label.checkbox`、`checkbox-assertive` 配色、多选独立状态切换及现代 `<ion-checkbox>` 映射。
运行 Ionic 1 Checkbox 列表 DOM、配色主题与多选状态机离线核验

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

当前源码 SHA256:867ad0ca667fd47b66bfe4ef379b5d1e959bb2c34af1de9ed9fcf7bc0eaec457

下载本站示例源码

本地运行

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

下载Ionic Checkbox 复选框实验

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

ZIP SHA256:595d252113b808857116bf084d0b6b15332e01d5e62e0b4f9cf8b1b6d93bdd0f

常见错误与边界

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

练习与解答

在 Ionic 1 列表中,`item-checkbox` 应该加在哪个元素上?`checkbox-assertive` 又应该加在哪个元素上?

展开参考解答

`item-checkbox` 加在列表项容器(如 `<li class="item item-checkbox">`)上以控制左侧勾选框的定位和内边距;颜色类 `checkbox-assertive` 加在包裹 `<input>` 的 `<label class="checkbox checkbox-assertive">` 上。

规范与官方依据