You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让:focus-within在苹果iOS及Safari桌面端正常生效?

纯CSS解决Safari中:focus-within点击不触发的下拉筛选器方案

方案1:使用原生
/控件

这是最简洁且符合可访问性的方案,原生控件自带展开/收起逻辑,无需额外JS或tabindex,Safari完美支持:

<details class="filter-dropdown">
  <summary class="filter-trigger">筛选选项</summary>
  <ul class="filter-options">
    <li><label><input type="checkbox" /> 选项1</label></li>
    <li><label><input type="checkbox" /> 选项2</label></li>
    <li><label><input type="checkbox" /> 选项3</label></li>
  </ul>
</details>
.filter-dropdown {
  position: relative;
  display: inline-block;
}

.filter-trigger {
  padding: 8px 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  list-style: none; /* 去掉默认展开箭头 */
}

/* 自定义展开箭头 */
.filter-trigger::marker {
  content: '';
}
.filter-trigger::after {
  content: '▼';
  margin-left: 8px;
  font-size: 0.8em;
}

.filter-dropdown[open] .filter-trigger::after {
  content: '▲';
}

.filter-options {
  position: absolute;
  top: 100%;
  left: 0;
  margin: 4px 0 0;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: white;
  list-style: none;
  display: none;
}

.filter-dropdown[open] .filter-options {
  display: block;
}

方案2:隐藏checkbox + label触发

通过label关联隐藏的checkbox,利用:checked状态控制下拉显示,同样纯CSS,兼容Safari:

<div class="filter-dropdown">
  <input type="checkbox" id="filter-toggle" class="filter-toggle" hidden>
  <label for="filter-toggle" class="filter-trigger">筛选选项</label>
  <ul class="filter-options">
    <li><label><input type="checkbox" /> 选项1</label></li>
    <li><label><input type="checkbox" /> 选项2</label></li>
    <li><label><input type="checkbox" /> 选项3</label></li>
  </ul>
</div>
.filter-dropdown {
  position: relative;
  display: inline-block;
}

.filter-trigger {
  padding: 8px 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  display: inline-block;
}

.filter-trigger::after {
  content: '▼';
  margin-left: 8px;
  font-size: 0.8em;
}

.filter-toggle:checked + .filter-trigger::after {
  content: '▲';
}

.filter-options {
  position: absolute;
  top: 100%;
  left: 0;
  margin: 4px 0 0;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: white;
  list-style: none;
  display: none;
}

.filter-toggle:checked ~ .filter-options {
  display: block;
}

原方案失效原因

Safari对按钮点击时的焦点传递逻辑与其他浏览器不同:点击

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:43:16