表单
赞踩投票
用户可将回答、帖子或评论的分数加一或减一。
本页内容
投票让每位读者可以为回答、帖子或评论加一分或减一分。两个箭头是同一组中的原生单选按钮,外观是低调的方形按钮。中间的分数由 CSS 计数器显示,按下箭头即可更新分数,无需脚本。
投票
<article class="sheet max-w(440) w-full flex items-start gap-sm p(12)"> <fieldset class="vote" aria-label="Vote on this answer" style="--votes: 41"> <input type="radio" class="vote-up" name="answer-vote" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="answer-vote" value="down" aria-label="Downvote"> </fieldset> <div class="flex flex-col gap(4) pt(6) pr(8)"> <p class="m(0) text-sm font-medium fg-text-strong">Put the focus ring on the group</p> <p class="m(0) text-sm fg-text-body"> A mask cuts the element it is drawn on, ring and all. Draw the ring on the container while one of its inputs has focus. </p> </div></article><article className="sheet max-w(440) w-full flex items-start gap-sm p(12)"> <fieldset className="vote" aria-label="Vote on this answer" style={{ '--votes': '41' } as React.CSSProperties}> <input type="radio" className="vote-up" name="answer-vote" value="up" aria-label="Upvote" /> <output className="vote-score"></output> <input type="radio" className="vote-down" name="answer-vote" value="down" aria-label="Downvote" /> </fieldset> <div className="flex flex-col gap(4) pt(6) pr(8)"> <p className="m(0) text-sm font-medium fg-text-strong">Put the focus ring on the group</p> <p className="m(0) text-sm fg-text-body"> A mask cuts the element it is drawn on, ring and all. Draw the ring on the container while one of its inputs has focus. </p> </div></article><article class="sheet max-w(440) w-full flex items-start gap-sm p(12)"> <fieldset class="vote" aria-label="Vote on this answer" style="--votes: 41"> <input type="radio" class="vote-up" name="answer-vote" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="answer-vote" value="down" aria-label="Downvote"> </fieldset> <div class="flex flex-col gap(4) pt(6) pr(8)"> <p class="m(0) text-sm font-medium fg-text-strong">Put the focus ring on the group</p> <p class="m(0) text-sm fg-text-body"> A mask cuts the element it is drawn on, ring and all. Draw the ring on the container while one of its inputs has focus. </p> </div></article><template> <article class="sheet max-w(440) w-full flex items-start gap-sm p(12)"> <fieldset class="vote" aria-label="Vote on this answer" style="--votes: 41"> <input type="radio" class="vote-up" name="answer-vote" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="answer-vote" value="down" aria-label="Downvote"> </fieldset> <div class="flex flex-col gap(4) pt(6) pr(8)"> <p class="m(0) text-sm font-medium fg-text-strong">Put the focus ring on the group</p> <p class="m(0) text-sm fg-text-body"> A mask cuts the element it is drawn on, ring and all. Draw the ring on the container while one of its inputs has focus. </p> </div> </article></template>按下一个箭头投票,按下另一个即可改投。
#分数
将组上的 --votes 设为不计当前读者投票的分数。选中向上箭头加一分,选中向下箭头减一分。如果页面知道读者投了哪一票,就选中对应的箭头,并将其他读者投出的分数作为基准。将 vote-score 留空,由计数器显示分数,负数使用真正的减号。如果分数太大,不适合以一分为单位显示,可在其中填写 1.4k 之类的文本。
#横向排列
vote-horizontal 将两个箭头放在分数两侧,适合评论下方的操作栏。分数预留三位数字的宽度,数值变化时箭头位置保持固定。
横向排列
<div class="card max-w(480) mx-auto"> <ol class="thread"> <li class="thread-item"> <span class="thread-avatar"><span class="avatar">KJ</span></span> <div class="thread-body"> <p class="thread-header"> <span class="thread-author">Katherine Johnson</span> <time class="thread-meta" datetime="2026-11-12T10:54">12m</time> </p> <p class="thread-text"> The figures in the second table are right. The third needs another pass. </p> <div class="flex items-center gap(4) mt(6) ml(-8)"> <fieldset class="vote vote-horizontal" aria-label="Vote on this comment" style="--votes: 12"> <input type="radio" class="vote-up" name="comment-vote" value="up" aria-label="Upvote" checked> <output class="vote-score"></output> <input type="radio" class="vote-down" name="comment-vote" value="down" aria-label="Downvote"> </fieldset> <button type="button" class="btn btn-quiet btn-sm">Reply</button> </div> </div> </li> </ol></div><div className="card max-w(480) mx-auto"> <ol className="thread"> <li className="thread-item"> <span className="thread-avatar"><span className="avatar">KJ</span></span> <div className="thread-body"> <p className="thread-header"> <span className="thread-author">Katherine Johnson</span> <time className="thread-meta" dateTime="2026-11-12T10:54">12m</time> </p> <p className="thread-text"> The figures in the second table are right. The third needs another pass. </p> <div className="flex items-center gap(4) mt(6) ml(-8)"> <fieldset className="vote vote-horizontal" aria-label="Vote on this comment" style={{ '--votes': '12' } as React.CSSProperties}> <input type="radio" className="vote-up" name="comment-vote" value="up" aria-label="Upvote" defaultChecked /> <output className="vote-score"></output> <input type="radio" className="vote-down" name="comment-vote" value="down" aria-label="Downvote" /> </fieldset> <button type="button" className="btn btn-quiet btn-sm">Reply</button> </div> </div> </li> </ol></div><div class="card max-w(480) mx-auto"> <ol class="thread"> <li class="thread-item"> <span class="thread-avatar"><span class="avatar">KJ</span></span> <div class="thread-body"> <p class="thread-header"> <span class="thread-author">Katherine Johnson</span> <time class="thread-meta" datetime="2026-11-12T10:54">12m</time> </p> <p class="thread-text"> The figures in the second table are right. The third needs another pass. </p> <div class="flex items-center gap(4) mt(6) ml(-8)"> <fieldset class="vote vote-horizontal" aria-label="Vote on this comment" style="--votes: 12"> <input type="radio" class="vote-up" name="comment-vote" value="up" aria-label="Upvote" checked> <output class="vote-score"></output> <input type="radio" class="vote-down" name="comment-vote" value="down" aria-label="Downvote"> </fieldset> <button type="button" class="btn btn-quiet btn-sm">Reply</button> </div> </div> </li> </ol></div><template> <div class="card max-w(480) mx-auto"> <ol class="thread"> <li class="thread-item"> <span class="thread-avatar"><span class="avatar">KJ</span></span> <div class="thread-body"> <p class="thread-header"> <span class="thread-author">Katherine Johnson</span> <time class="thread-meta" datetime="2026-11-12T10:54">12m</time> </p> <p class="thread-text"> The figures in the second table are right. The third needs another pass. </p> <div class="flex items-center gap(4) mt(6) ml(-8)"> <fieldset class="vote vote-horizontal" aria-label="Vote on this comment" style="--votes: 12"> <input type="radio" class="vote-up" name="comment-vote" value="up" aria-label="Upvote" checked> <output class="vote-score"></output> <input type="radio" class="vote-down" name="comment-vote" value="down" aria-label="Downvote"> </fieldset> <button type="button" class="btn btn-quiet btn-sm">Reply</button> </div> </div> </li> </ol> </div></template>#状态
未悬停、按下或选中时,箭头仅显示柔和的墨色轮廓,按钮没有背景。指针悬停时,按钮呈现为小型凸起面板,按下时沉入凹槽,选中后箭头以墨色填实。两个箭头通过朝向区分,无需依赖颜色。对于无法投票的分数,例如读者自己帖子的分数,请禁用 fieldset。此时箭头变淡,分数仍然可见。
状态
<fieldset class="vote" aria-label="Vote on this answer" style="--votes: 3"> <input type="radio" class="vote-up" name="vote-down-state" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-down-state" value="down" aria-label="Downvote" checked></fieldset><fieldset class="vote" aria-label="Vote on this answer" style="--votes: -2"> <input type="radio" class="vote-up" name="vote-negative" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-negative" value="down" aria-label="Downvote"></fieldset><fieldset class="vote" aria-label="Votes on your answer" disabled> <input type="radio" class="vote-up" name="vote-own" value="up" aria-label="Upvote"> <output class="vote-score">1.4k</output> <input type="radio" class="vote-down" name="vote-own" value="down" aria-label="Downvote"></fieldset><> <fieldset className="vote" aria-label="Vote on this answer" style={{ '--votes': '3' } as React.CSSProperties}> <input type="radio" className="vote-up" name="vote-down-state" value="up" aria-label="Upvote" /> <output className="vote-score"></output> <input type="radio" className="vote-down" name="vote-down-state" value="down" aria-label="Downvote" defaultChecked /> </fieldset> <fieldset className="vote" aria-label="Vote on this answer" style={{ '--votes': '-2' } as React.CSSProperties}> <input type="radio" className="vote-up" name="vote-negative" value="up" aria-label="Upvote" /> <output className="vote-score"></output> <input type="radio" className="vote-down" name="vote-negative" value="down" aria-label="Downvote" /> </fieldset> <fieldset className="vote" aria-label="Votes on your answer" disabled> <input type="radio" className="vote-up" name="vote-own" value="up" aria-label="Upvote" /> <output className="vote-score">1.4k</output> <input type="radio" className="vote-down" name="vote-own" value="down" aria-label="Downvote" /> </fieldset></><fieldset class="vote" aria-label="Vote on this answer" style="--votes: 3"> <input type="radio" class="vote-up" name="vote-down-state" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-down-state" value="down" aria-label="Downvote" checked></fieldset><fieldset class="vote" aria-label="Vote on this answer" style="--votes: -2"> <input type="radio" class="vote-up" name="vote-negative" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-negative" value="down" aria-label="Downvote"></fieldset><fieldset class="vote" aria-label="Votes on your answer" disabled> <input type="radio" class="vote-up" name="vote-own" value="up" aria-label="Upvote"> <output class="vote-score">1.4k</output> <input type="radio" class="vote-down" name="vote-own" value="down" aria-label="Downvote"></fieldset><template> <fieldset class="vote" aria-label="Vote on this answer" style="--votes: 3"> <input type="radio" class="vote-up" name="vote-down-state" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-down-state" value="down" aria-label="Downvote" checked> </fieldset> <fieldset class="vote" aria-label="Vote on this answer" style="--votes: -2"> <input type="radio" class="vote-up" name="vote-negative" value="up" aria-label="Upvote"> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-negative" value="down" aria-label="Downvote"> </fieldset> <fieldset class="vote" aria-label="Votes on your answer" disabled> <input type="radio" class="vote-up" name="vote-own" value="up" aria-label="Upvote"> <output class="vote-score">1.4k</output> <input type="radio" class="vote-down" name="vote-own" value="down" aria-label="Downvote"> </fieldset></template>#撤回投票
再次按下已选中的单选按钮无法取消选中,因此没有脚本时,读者只能改投。vote 行为脚本让读者可以再次按下已选中的箭头,或在该箭头获得焦点时按空格键,撤回投票并将分数恢复为 --votes。
撤回投票
<fieldset class="vote" id="vote-clear" aria-label="Vote on this answer" style="--votes: 7"> <input type="radio" class="vote-up" name="vote-clear" value="up" aria-label="Upvote" checked> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-clear" value="down" aria-label="Downvote"></fieldset><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLFieldSetElement>(null) useEffect(() => attach(root.current!), []) return ( <fieldset ref={root} className="vote" id="vote-clear" aria-label="Vote on this answer" style={{ '--votes': '7' } as React.CSSProperties}> <input type="radio" className="vote-up" name="vote-clear" value="up" aria-label="Upvote" defaultChecked /> <output className="vote-score"></output> <input type="radio" className="vote-down" name="vote-clear" value="down" aria-label="Downvote" /> </fieldset> )}<script> import { attach } from '@master/ui/behaviors'</script><fieldset class="vote" id="vote-clear" aria-label="Vote on this answer" style="--votes: 7" {@attach attach}> <input type="radio" class="vote-up" name="vote-clear" value="up" aria-label="Upvote" checked> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-clear" value="down" aria-label="Downvote"></fieldset><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <fieldset ref="root" class="vote" id="vote-clear" aria-label="Vote on this answer" style="--votes: 7"> <input type="radio" class="vote-up" name="vote-clear" value="up" aria-label="Upvote" checked> <output class="vote-score"></output> <input type="radio" class="vote-down" name="vote-clear" value="down" aria-label="Downvote"> </fieldset></template>再次按下实心箭头即可撤回投票。
#无障碍
- 用
aria-label或<legend>为单选按钮组提供无障碍名称,并分别将两个单选按钮命名为“赞成”和“反对”。 - 将分数放在
<output>中。辅助技术按内容顺序读取,在读完第一个箭头后、读取第二个箭头前读出分数。投票改变分数时,还会将新分数作为状态更新播报。 - 箭头图形绘制在伪元素上,避免遮罩裁掉焦点环。与其他单选按钮组一样,方向键可在两个单选按钮之间切换投票。
- 无论使用哪种指针设备,箭头按钮的尺寸均为 32px。在触摸屏上,按钮周围的触控目标扩大到 44px。在强制颜色模式下,箭头图形使用按钮文字颜色,选中的箭头使用高亮颜色。
#类名
| 类名 | 说明 |
|---|---|
vote | 应用于通过 aria-label 或 legend 命名的 fieldset,依次纵向排列向上箭头、分数和向下箭头。将 --votes 设为未计入当前读者投票的分数。 |
vote-horizontal | 分数两侧的箭头,用于评论下方的操作行。分数固定占三个数字的宽度。与 vote 一同使用 |
vote-up | 向上箭头是一个无障碍名称为“Upvote”的单选按钮,外观低调,呈 32px 正方形。选中时,箭头以墨色填充。 |
vote-score | 元素为空时,计数器写入分数,即 --votes、比它多一分或少一分。在 output 元素上,分数变化时会播报。元素内已有文本时(如 1.2k),显示该文本。 |
vote-down | 向下箭头是一个单选按钮,与向上箭头共用同一个 name 值,无障碍名称为“Downvote”。 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--votes | 0 | 未计入读者投票的分数。选中向上或向下的箭头时,分数加一或减一。 |
--vote-size | 2rem | 每个箭头控件的正方形边长。在触摸屏上,包含箭头控件在内的整个触控目标尺寸为 44px。 |