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

jQuery Mobile 可折叠块

详解 jQuery Mobile 可折叠内容块(data-role="collapsible")、默认展开控制(data-collapsed="false")、多级嵌套折叠、手风琴互斥集合(data-role="collapsible-set")以及外观与图标定制属性,并与原生 HTML5 <details> 对照。

学完这一章,你能够

  • 详解 jQuery Mobile 可折叠内容块(data-role="collapsible")、默认展开控制(data-collapsed="false")、多级嵌套折叠、手风琴互斥集合(data-role="collapsible-set")以及外观与图标定制属性,并与原生 HTML5 <details> 对照。

基础可折叠块与默认展开控制

可折叠块(Collapsible)能够在有限的移动屏幕内按需隐藏或展开局部信息。创建方式是为 `<div>` 容器添加 `data-role="collapsible"`,并在内部首行放置任意一级标题标签(`<h1>`–`<h6>`)作为可点击的折叠栏标题,其后跟随待展开的 HTML 内容。可折叠块默认处于收起状态;若希望页面载入时即展开,添加 `data-collapsed="false"` 即可。

多级嵌套与手风琴可折叠集合

可折叠块支持按业务层次进行多次嵌套,只需在父折叠块的内容区再放入子 `<div data-role="collapsible">` 即可。若希望多个并列折叠项呈现“手风琴”互斥交互(展开新项时自动收起其余项),将它们统一包裹在 `<div data-role="collapsible-set">` 容器内即可。

外观定制选项与 HTML5 原生替代

可折叠块与折叠集合还提供丰富的声明式样式选项:`data-inset="false"` 可取消外边距与圆角以贴合整屏宽度;`data-mini="true"` 生成紧凑尺寸;`data-collapsed-icon` 与 `data-expanded-icon` 可替换默认的 `+`/`-` 状态图标;`data-iconpos` 可修改图标位置(默认靠左);并且折叠块也可嵌套进弹窗(Popup)中使用。在 jQuery Mobile 已于 2021 年停止维护的今天,现代浏览器已原生支持 `<details>` 与 `<summary>`(配合相同 `name` 属性还可零脚本实现互斥手风琴),推荐新项目优先采用原生方案。

可折叠块嵌套能力提示

主题结构说明
多级嵌套支持可折叠内容块允许按层级多次嵌套,只需保证每一级容器都带 data-role="collapsible" 并包含标题元素(H1–H6)。

运行可折叠块、嵌套结构、手风琴集合及原生 <details> 离线核验

含完整源码 · 124 行
展开查看收起
python
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 12: collapsibles."""
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: Basic collapsible (default collapsed) vs data-collapsed="false" (expanded)
    doc1 = html.fragment_fromstring(
        """
        <div>
          <div data-role="collapsible" id="c-default">
            <h1>点击展开默认折叠块</h1>
            <p>我是可折叠的内容。</p>
          </div>
          <div data-role="collapsible" id="c-open" data-collapsed="false">
            <h1>默认展开的可折叠块</h1>
            <p>页面加载时已默认展开。</p>
          </div>
        </div>
        """
    )
    c_def, c_open = doc1.xpath("./div[@data-role='collapsible']")
    actual_1 = {
        "default_collapsed": c_def.get("data-collapsed", "true") == "true",
        "default_has_heading": bool(c_def.xpath("./h1|./h2|./h3|./h4|./h5|./h6")),
        "open_collapsed": c_open.get("data-collapsed") == "false",
    }
    expected_1 = {
        "default_collapsed": True,
        "default_has_heading": True,
        "open_collapsed": True,
    }
    cases.append({
        "name": "collapsible_basic_and_expanded",
        "summary": "Verify data-role='collapsible' heading child requirement, default collapsed state, and data-collapsed='false'.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Nested collapsibles depth check
    doc2 = html.fragment_fromstring(
        """
        <div data-role="collapsible" id="outer">
          <h1>外层折叠块</h1>
          <p>外层说明段落。</p>
          <div data-role="collapsible" id="inner">
            <h2>内层嵌套折叠块</h2>
            <p>内层展开内容。</p>
          </div>
        </div>
        """
    )
    inner = doc2.xpath(".//div[@data-role='collapsible']")[0]
    actual_2 = {
        "outer_id": doc2.get("id"),
        "inner_id": inner.get("id"),
        "nested_collapsible_count": len(doc2.xpath(".//div[@data-role='collapsible']")),
    }
    expected_2 = {
        "outer_id": "outer",
        "inner_id": "inner",
        "nested_collapsible_count": 1,
    }
    cases.append({
        "name": "collapsible_nesting",
        "summary": "Verify nested data-role='collapsible' containers inside a parent collapsible block.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Accordion collapsible-set + style/icon customization + native HTML5 <details> equivalent
    doc3 = html.fragment_fromstring(
        """
        <div>
          <div data-role="collapsible-set" data-inset="false" data-mini="true"
               data-collapsed-icon="arrow-d" data-expanded-icon="arrow-u" data-iconpos="right">
            <div data-role="collapsible"><h1>手风琴第一项</h1><p>内容一</p></div>
            <div data-role="collapsible"><h1>手风琴第二项</h1><p>内容二</p></div>
          </div>
          <details name="acc" open><summary>原生 HTML5 折叠项</summary><p>原生免脚本展开</p></details>
        </div>
        """
    )
    cset = doc3.xpath("./div[@data-role='collapsible-set']")[0]
    actual_3 = {
        "child_collapsibles": len(cset.xpath("./div[@data-role='collapsible']")),
        "inset": cset.get("data-inset"),
        "mini": cset.get("data-mini"),
        "icons": [cset.get("data-collapsed-icon"), cset.get("data-expanded-icon")],
        "iconpos": cset.get("data-iconpos"),
        "native_details_open": doc3.xpath("./details")[0].get("open") is not None,
    }
    expected_3 = {
        "child_collapsibles": 2,
        "inset": "false",
        "mini": "true",
        "icons": ["arrow-d", "arrow-u"],
        "iconpos": "right",
        "native_details_open": True,
    }
    cases.append({
        "name": "collapsible_set_and_options",
        "summary": "Verify data-role='collapsible-set' accordion grouping, inset/mini/icon customization, and HTML5 <details> counterpart.",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    return cases


def main():
    print(json.dumps({"version": VERSION, "cases": run_cases()}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
确认默认收起与 data-collapsed="false" 展开、嵌套折叠深度、collapsible-set 定制属性(inset/mini/icons/iconpos)以及原生 HTML5 <details> 对照。

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

本地运行

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

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

本章附件与实例包

下载课程关联工程包:jQuery Mobile

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

下载课程关联工程包:jQuery Mobile

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

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

单独并列时每个折叠块相互独立,可同时展开多项;放入 data-role="collapsible-set" 后形成互斥手风琴集合,展开其中一项时其余项会自动收起。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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