Foundation 提醒框
核对 Foundation 5 提醒框(.alert-box)的满宽布局与六种语境色、.radius/.round 圆角变体、关闭按钮 .close 与 Foundation JS 交互移除机制,以及 × 字符实体码点验证。
学完这一章,你能够
- 核对 Foundation 5 提醒框(.alert-box)的满宽布局与六种语境色、.radius/.round 圆角变体、关闭按钮 .close 与 Foundation JS 交互移除机制,以及 × 字符实体码点验证。
提醒框基础结构与六种语境样式
Foundation 5 使用带有 `data-alert` 属性与 `.alert-box` 类的 `<div>` 创建页面提醒框,并可叠加 `.secondary`、`.success`、`.info`、`.warning` 或 `.alert` 区分反馈级别。提醒框默认为块级元素并占据父容器 `100%` 宽度。本站离线 Playwright Chromium 在 `600px` 容器内实测全部 6 种提醒框宽度均为 `600px`,配色依次为:默认蓝 `rgb(0, 140, 186)`/白字、`.secondary` `rgb(231, 231, 231)`/深灰 `rgb(79, 79, 79)`、`.success` `rgb(67, 172, 106)`/白字、`.info` `rgb(160, 211, 232)`/深灰 `rgb(79, 79, 79)`、`.warning` `rgb(240, 138, 36)`/白字、`.alert` `rgb(240, 65, 36)`/白字。
圆角提醒框(.radius / .round)
默认提醒框四角为直角(`0px`);在 `.alert-box` 上附加 `.radius` 或 `.round` 类可分别切换为小圆角与胶囊大圆角。本站离线实测 `.alert-box.success.radius` 的 `border-radius` 为 `3px`,`.alert-box.info.round` 为 `1000px`。
可关闭提醒框与 × 实体解析
若要允许用户手动关闭提醒框,可在提醒框内部添加 `<a href="#" class="close">×</a>`(或同类按钮)并在页面初始化 `$(document).foundation()`。需注意 `×` 是 HTML 字符实体,渲染为对称的乘号图标 `×` 而非小写英文字母 `x`。本站离线 Chromium 实测验证:`×` 对应的真实 Unicode 字符为 `×`(码点 `215`,非 ASCII `x` 的 `120`);点击第一条提醒框的 `.close` 链接后,测试区内 `[data-alert]` 数量由 `2` 降至 `1`,目标节点被移除且同级第二条提醒框保持完好。
提醒框宽度布局提示
| 提示主题 | 核心说明 |
|---|---|
| 提醒框的默认宽度 | `.alert-box` 默认作为块级容器占据父元素 100% 宽度(实测 600px 容器下全部六种提醒框宽度均为 600px);如需限定宽度可将其放入网格列中。 |
关闭符号 × 字符实体提示
| 提示主题 | 核心说明 |
|---|---|
| 为何使用 × 而非字母 x? | `×` 解析为对称乘号 `×`(Unicode 码点 U+00D7 / 215),视觉上比普通英文字母 `x`(码点 120)更工整,适合用作关闭图标。 |
离线验证 Foundation 5 提醒框满宽配色、圆角、× 码点与点击关闭移除交互
"""Offline Playwright Chromium lab for Foundation Ch 09 (alerts)."""
import json
from playwright.sync_api import sync_playwright
FOUNDATION_ALERT_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; }
.wrap { width: 600px; }
.alert-box {
border: 1px solid #0078a0; display: block; font-size: 0.8125rem; font-weight: normal;
margin-bottom: 1.25rem; padding: 0.875rem 1.5rem 0.875rem 0.875rem; position: relative;
background-color: #008CBA; color: #ffffff; width: 100%; border-radius: 0px;
}
.alert-box.secondary { background-color: #e7e7e7; border-color: #c7c7c7; color: #4f4f4f; }
.alert-box.success { background-color: #43AC6A; border-color: #3a945b; color: #ffffff; }
.alert-box.info { background-color: #a0d3e8; border-color: #74bfdd; color: #4f4f4f; }
.alert-box.warning { background-color: #f08a24; border-color: #de770f; color: #ffffff; }
.alert-box.alert { background-color: #f04124; border-color: #de2d0f; color: #ffffff; }
.alert-box.radius { border-radius: 3px; }
.alert-box.round { border-radius: 1000px; }
.alert-box .close {
right: 0.25rem; top: 50%; margin-top: -0.6875rem; position: absolute;
font-size: 1.375rem; line-height: 0.9; opacity: 0.3; text-decoration: none; color: #333333; cursor: pointer;
}
"""
HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_ALERT_CSS}</style></head>
<body>
<div class="wrap">
<div id="a-default" data-alert class="alert-box">This is a default alert box.</div>
<div id="a-secondary" data-alert class="alert-box secondary">This is a secondary alert box.</div>
<div id="a-success" data-alert class="alert-box success"><strong>Success!</strong> Positive action.</div>
<div id="a-info" data-alert class="alert-box info"><strong>Info!</strong> Neutral change.</div>
<div id="a-warning" data-alert class="alert-box warning"><strong>Warning!</strong> Needs attention.</div>
<div id="a-alert" data-alert class="alert-box alert"><strong>Alert!</strong> Dangerous action.</div>
<div id="a-radius" data-alert class="alert-box success radius"><strong>Success!</strong> Radius alert.</div>
<div id="a-round" data-alert class="alert-box info round"><strong>Info!</strong> Round alert.</div>
<div id="closable-zone">
<div id="a-close-1" data-alert class="alert-box">
Closable alert box 1.
<a id="close-btn-1" href="#" class="close">×</a>
</div>
<div id="a-close-2" data-alert class="alert-box success">
Closable alert box 2.
<a id="close-btn-2" href="#" class="close">×</a>
</div>
</div>
</div>
<script>
document.querySelectorAll('[data-alert] .close').forEach((btn) => {{
btn.addEventListener('click', (e) => {{
e.preventDefault();
const box = btn.closest('[data-alert]');
if (box) box.remove();
}});
}});
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 800, "height": 900})
page.set_content(HTML_PAGE)
# Case 1: 6 alert variants & 100% container width
actual_1 = page.evaluate("""() => {
const ids = ['a-default','a-secondary','a-success','a-info','a-warning','a-alert'];
const variants = {};
const widths = [];
for (const id of ids) {
const el = document.getElementById(id);
const s = getComputedStyle(el);
variants[id.slice(2)] = { bg: s.backgroundColor, color: s.color };
widths.push(Math.round(el.getBoundingClientRect().width));
}
return { variants, widths };
}""")
expected_1 = {
"variants": {
"default": {"bg": "rgb(0, 140, 186)", "color": "rgb(255, 255, 255)"},
"secondary": {"bg": "rgb(231, 231, 231)", "color": "rgb(79, 79, 79)"},
"success": {"bg": "rgb(67, 172, 106)", "color": "rgb(255, 255, 255)"},
"info": {"bg": "rgb(160, 211, 232)", "color": "rgb(79, 79, 79)"},
"warning": {"bg": "rgb(240, 138, 36)", "color": "rgb(255, 255, 255)"},
"alert": {"bg": "rgb(240, 65, 36)", "color": "rgb(255, 255, 255)"},
},
"widths": [600, 600, 600, 600, 600, 600],
}
cases.append({
"name": "alert_box_six_variants_and_full_width",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: .radius and .round alert corners
actual_2 = page.evaluate("""() => ({
default_radius: getComputedStyle(document.getElementById('a-default')).borderRadius,
radius_class: getComputedStyle(document.getElementById('a-radius')).borderRadius,
round_class: getComputedStyle(document.getElementById('a-round')).borderRadius
})""")
expected_2 = {
"default_radius": "0px",
"radius_class": "3px",
"round_class": "1000px",
}
cases.append({
"name": "alert_box_radius_and_round",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: × HTML entity code point vs ASCII 'x'
actual_3 = page.evaluate("""() => {
const txt = document.getElementById('close-btn-1').textContent;
return {
rendered_char: txt,
code_point: txt.charCodeAt(0),
is_ascii_x: txt === 'x'
};
}""")
expected_3 = {
"rendered_char": "×",
"code_point": 215,
"is_ascii_x": False,
}
cases.append({
"name": "times_entity_codepoint_verification",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: clicking .close removes target [data-alert] from DOM
before_count = page.evaluate("() => document.querySelectorAll('#closable-zone [data-alert]').length")
page.click("#close-btn-1")
after_4 = page.evaluate("""() => ({
before_count: 2,
after_count: document.querySelectorAll('#closable-zone [data-alert]').length,
first_removed: !document.getElementById('a-close-1'),
second_preserved: !!document.getElementById('a-close-2')
})""")
after_4["before_count"] = before_count
expected_4 = {
"before_count": 2,
"after_count": 1,
"first_removed": True,
"second_preserved": True,
}
cases.append({
"name": "alert_box_close_interaction",
"expected": expected_4,
"actual": after_4,
"passed": after_4 == expected_4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 在离线 Chromium 中核对 600px 满宽 6 种配色提醒框、0px/3px/1000px 圆角、× 解析为 U+00D7(215)及点击 .close 后仅移除目标 [data-alert] 节点。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:751e9588fe81611a7e1aaa93f158769f34c844370971dcad3fe8430e5f4d8dd6
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:5eda23e81747f36cd8c30246b30d0ff5f3166a61abb77352b4f3c6b7efc2b91e
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
构建可关闭的 Foundation 5 提醒框必须同时满足哪三个要素?
展开参考解答
一是在提醒框容器上保留 data-alert 属性与 .alert-box 类;二是在内部放置带 class="close" 的链接或按钮(通常内容为 ×);三是在页面加载完成后调用 $(document).foundation() 初始化 Alert 插件事件绑定。