蓝色天空.知识 · 实践 · 创造
我的收藏
Ionic 教程目录 37章
Ionic / LEARN BY DOING

Ionic 表单和输入框

核对 Ionic 1 表单输入框的全部八种常用布局:纯 placeholder、左侧 input-label、堆叠标签、动画浮动标签、list-inset 内嵌表单、item-input-inset 带按钮输入域、图标搜索框及头部搜索栏。

学完这一章,你能够

  • 核对 Ionic 1 表单输入框的全部八种常用布局:纯 placeholder、左侧 input-label、堆叠标签、动画浮动标签、list-inset 内嵌表单、item-input-inset 带按钮输入域、图标搜索框及头部搜索栏。

基础输入框与左侧标签(placeholder / input-label)

Ionic 1 沿用列表容器 `.list` 组织表单字段,每行由 `<label class="item item-input">` 包裹具体输入控件(支持 `<input>` 与 `<textarea>`)。最简洁的形式是通栏 100% 宽度且无左右外边框的纯占位符模式,借助 HTML5 `placeholder` 提示预期输入;若需要明确字段名,则在 `<input>` 前添加 `<span class="input-label">` 将标签固定在左侧,形成标准的“左标签 + 右输入”布局(如用户名与密码框)。

堆叠标签与浮动标签(Stacked / Floating Label)

在窄屏手机上字段标题较长时,可使用堆叠标签:给 `<label>` 追加 `item-stacked-label` 类并保留 `<span class="input-label">` 与 `placeholder`,标签将固定在输入区域上方。若希望兼顾简洁界面与输入时的字段提醒,可改用 `item-floating-label` 浮动标签——未输入内容时仅显示占位符,当用户开始键入文本后,标签以平滑动画向上浮现于输入框上方。

内嵌表单、行内按钮域、图标输入框与头部搜索栏

如果不想表单控件撑满整个屏幕宽度,可以给外层容器加上 `list list-inset`(留白无阴影)或 `.card`(留白带阴影)形成内嵌表单。要在单个输入行右侧并排嵌入提交按钮,需将行容器写成 `<div class="item item-input-inset">`,用 `<label class="item-input-wrapper">` 包裹 `<input>`,再紧跟 `<button class="button button-small">`。

为输入框增加搜索等前置图标十分直接:在 `<input>` 前放入 `<i class="icon ion-search placeholder-icon"></i>` 即可让图标与占位文字对齐。进一步将 `item-input-inset` 叠加到顶部栏 `<div class="bar bar-header item-input-inset">` 上,内部配合 `item-input-wrapper`、`input[type="search"]` 与右侧 `button-clear` 取消按钮,即可实现移动端经典的头部搜索条。在现代 Ionic 7/8 中,上述输入模式对应 `<ion-input label="..." label-placement="fixed|stacked|floating">`、`<ion-textarea>` 与 `<ion-searchbar>`。

运行 Ionic 1 八类表单输入框布局与浮动标签状态离线核验

含完整源码 · 223 行
展开查看收起
python
"""Offline Ionic 1 Form & Input layouts (placeholder, input-label, stacked, floating, inset, search) verifier for Ch08 (form-input)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"


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


def case_placeholder_and_inline_label() -> dict:
    markup = """
    <section>
      <div class="list">
        <label class="item item-input"><input type="text" placeholder="First Name"></label>
        <label class="item item-input"><input type="text" placeholder="Last Name"></label>
        <label class="item item-input"><textarea placeholder="Comments"></textarea></label>
      </div>
      <div class="list">
        <label class="item item-input">
          <span class="input-label">用户名:</span>
          <input type="text">
        </label>
        <label class="item item-input">
          <span class="input-label">密码:</span>
          <input type="password">
        </label>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    lists = root.xpath("./div[@class='list']")
    placeholders = lists[0].xpath(".//*[@placeholder]/@placeholder")
    control_tags = [el.tag for el in lists[0].xpath(".//label/*")]
    inline_labels = [s.text_content().strip() for s in lists[1].xpath(".//span[@class='input-label']")]
    input_types = lists[1].xpath(".//input/@type")
    actual = {
        "placeholders": placeholders,
        "placeholder_controls": control_tags,
        "inline_labels": inline_labels,
        "inline_input_types": input_types,
    }
    expected = {
        "placeholders": ["First Name", "Last Name", "Comments"],
        "placeholder_controls": ["input", "input", "textarea"],
        "inline_labels": ["用户名:", "密码:"],
        "inline_input_types": ["text", "password"],
    }
    return make_case(
        "form_placeholder_and_inline_input_label",
        "Verify placeholder-only inputs/textarea and left-aligned .input-label text/password rows",
        expected,
        actual,
    )


def case_stacked_and_floating_labels() -> dict:
    markup = """
    <section>
      <div class="list">
        <label class="item item-input item-stacked-label">
          <span class="input-label">First Name</span>
          <input type="text" placeholder="John">
        </label>
        <label class="item item-input item-stacked-label">
          <span class="input-label">Last Name</span>
          <input type="text" placeholder="Suhr">
        </label>
        <label class="item item-input item-stacked-label">
          <span class="input-label">Email</span>
          <input type="text" placeholder="john@suhr.com">
        </label>
      </div>
      <div class="list">
        <label class="item item-input item-floating-label">
          <span class="input-label">First Name</span>
          <input type="text" placeholder="First Name">
        </label>
        <label class="item item-input item-floating-label">
          <span class="input-label">Last Name</span>
          <input type="text" placeholder="Last Name">
        </label>
        <label class="item item-input item-floating-label">
          <span class="input-label">Email</span>
          <input type="text" placeholder="Email">
        </label>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    stacked = root.xpath(".//label[contains(@class,'item-stacked-label')]")
    floating = root.xpath(".//label[contains(@class,'item-floating-label')]")

    # Simulate floating label CSS state (.has-input added when value is non-empty)
    def floating_state(value: str) -> dict:
        has_input = bool(value)
        return {"has_input_class": has_input, "label_opacity": 1.0 if has_input else 0.0}

    actual = {
        "stacked_count": len(stacked),
        "floating_count": len(floating),
        "floating_empty_state": floating_state(""),
        "floating_filled_state": floating_state("Ada"),
    }
    expected = {
        "stacked_count": 3,
        "floating_count": 3,
        "floating_empty_state": {"has_input_class": False, "label_opacity": 0.0},
        "floating_filled_state": {"has_input_class": True, "label_opacity": 1.0},
    }
    return make_case(
        "form_stacked_and_floating_labels",
        "Verify item-stacked-label and item-floating-label markup and floating label visibility state",
        expected,
        actual,
    )


def case_inset_form_and_inset_input() -> dict:
    markup = """
    <section>
      <div class="list list-inset">
        <label class="item item-input"><input type="text" placeholder="First Name"></label>
        <label class="item item-input"><input type="text" placeholder="Last Name"></label>
      </div>
      <div class="list">
        <div class="item item-input-inset">
          <label class="item-input-wrapper">
            <input type="text" placeholder="Email">
          </label>
          <button class="button button-small">Submit</button>
        </div>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    inset_list_inputs = len(root.xpath("./div[contains(@class,'list-inset')]//input"))
    inset_item = root.xpath(".//div[contains(@class,'item-input-inset')]")[0]
    actual = {
        "inset_form_input_count": inset_list_inputs,
        "has_input_wrapper": bool(inset_item.xpath("./label[@class='item-input-wrapper']/input")),
        "inline_button_text": inset_item.xpath("./button/text()")[0].strip(),
        "inline_button_small": "button-small" in inset_item.xpath("./button/@class")[0].split(),
    }
    expected = {
        "inset_form_input_count": 2,
        "has_input_wrapper": True,
        "inline_button_text": "Submit",
        "inline_button_small": True,
    }
    return make_case(
        "form_inset_list_and_input_inset_button",
        "Verify list list-inset form and item-input-inset + item-input-wrapper + button-small layout",
        expected,
        actual,
    )


def case_icon_and_header_search_input() -> dict:
    markup = """
    <section>
      <div class="list list-inset">
        <label class="item item-input">
          <i class="icon ion-search placeholder-icon"></i>
          <input type="text" placeholder="Search">
        </label>
      </div>
      <div class="bar bar-header item-input-inset">
        <label class="item-input-wrapper">
          <i class="icon ion-ios-search placeholder-icon"></i>
          <input type="search" placeholder="搜索">
        </label>
        <button class="button button-clear">取消</button>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    list_icon_before_input = root.xpath("./div[1]//label/*")[0].tag == "i"
    hdr = root.xpath("./div[contains(@class,'bar-header')]")[0]
    actual = {
        "list_icon_precedes_input": list_icon_before_input,
        "list_icon_has_placeholder_class": "placeholder-icon" in root.xpath("./div[1]//i/@class")[0].split(),
        "header_has_item_input_inset": "item-input-inset" in hdr.get("class", "").split(),
        "header_input_type": hdr.xpath(".//input/@type")[0],
        "header_cancel_button": hdr.xpath("./button/text()")[0].strip(),
    }
    expected = {
        "list_icon_precedes_input": True,
        "list_icon_has_placeholder_class": True,
        "header_has_item_input_inset": True,
        "header_input_type": "search",
        "header_cancel_button": "取消",
    }
    return make_case(
        "form_icon_and_header_search_bar",
        "Verify placeholder-icon placement before <input> and bar-header item-input-inset search bar",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_placeholder_and_inline_label(),
        case_stacked_and_floating_labels(),
        case_inset_form_and_inset_input(),
        case_icon_and_header_search_input(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
验证 placeholder 文本域与左侧 `input-label`、`item-stacked-label` 与 `item-floating-label` 状态切换、`list-inset` 与 `item-input-inset` 内嵌按钮,以及 `placeholder-icon` 和头部搜索栏结构。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

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

下载本章实验包 (mbskycom202612a2013.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Ionic

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip

下载课程关联工程包:Ionic

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2019.zip

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

`item-stacked-label` 始终将标签显示在输入框顶部,`item-floating-label` 在输入为空时隐藏标签仅显示 placeholder,输入后动画上浮显示标签;头部搜索栏使用 `bar bar-header item-input-inset` 搭配 `item-input-wrapper`、`placeholder-icon`、`input[type="search"]` 与 `button-clear`。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习