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

Safari中<a>标签点击失效:头部下拉菜单无法展开问题求助

解决Safari浏览器下拉菜单无法点击的问题

问题分析

下拉菜单在Chrome正常但Safari中标签完全不可点击,替换为span也无效,核心原因包括:

解决方案

1. 修复Header定位

给header添加position: fixed,确保定位和层级属性生效:

header {
  position: fixed; /* 添加该行 */
  top: 0;
  left: 0;
  right: 0;
  background: #ffffff;
  padding: 0px 7%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10000;
  /* 其余样式保持不变 */
}

2. 修正CSS语法错误

将.navbar ul li ul中的align-items: flex改为合法值(如flex-start):

.navbar ul li ul {
  position: absolute;
  left: 0;
  width: 250px;
  background: #ffffff;
  display: none;
  align-items: flex-start; /* 修正属性值 */
}

3. 启用:focus-within增强兼容性

恢复focus-within选择器,配合hover确保Safari中能正常触发下拉:

.navbar ul li:focus-within > ul,
.navbar ul li:hover > ul {
  display: initial;
  z-index: 100;
}

4. 优化无href的标签

给无跳转的标签添加href="javascript:void(0)",让Safari识别为可交互元素:

<li style="cursor: pointer;"><a href="javascript:void(0)">{{'header.second' | translate}} </a>
<!-- 其余代码保持不变 -->
<li style="cursor: pointer;"><a href="javascript:void(0)">{{'header.fourth' | translate}}</a>
<!-- 其余代码保持不变 -->

修改后的完整代码示例

CSS代码

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #ffffff;
  padding: 0px 7%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10000;
  .navbar ul {
    list-style: none;
  }
  .navbar ul li {
    position: relative;
    float: left;
  }
  .navbar ul li a,
  span {
    padding: 20px;
    color: #111d5e !important;
    font-weight: 600;
    text-decoration: none;
    display: block;
  }
  .navbar ul li a:after {
    background: none repeat scroll 0 0 transparent;
    bottom: 0;
    content: "";
    display: block;
    height: 2px;
    box-shadow: inset 3000px 0 0 0 #eed484;
    transition: width 0.3s ease 0s, left 0.3s ease 0s;
    width: 0;
  }
  .navbar ul li a:hover:after {
    width: 100%;
    left: 0;
  }
  .navbar ul li span:after {
    background: none repeat scroll 0 0 transparent;
    bottom: 0;
    content: "";
    display: block;
    height: 2px;
    box-shadow: inset 3000px 0 0 0 #eed484;
    transition: width 0.3s ease 0s, left 0.3s ease 0s;
    width: 0;
  }
  .navbar ul li span:hover:after {
    width: 100%;
    left: 0;
  }
  .navbar ul li ul li span,
  a {
    width: 100%;
  }
  .navbar ul li ul {
    position: absolute;
    left: 0;
    width: 250px;
    background: #ffffff;
    display: none;
    align-items: flex-start;
  }
  .navbar ul li ul li {
    width: 100%;
  }
  .navbar ul li:focus-within > ul,
  .navbar ul li:hover>ul {
    display: initial;
    z-index: 100;
  }
  #menu-bar {
    display: none;
  }
  label {
    font-size: 20px;
    color: #111d5e !important;
    cursor: pointer;
    display: none
  }
  .navbar ul li .language {
    width: 155px;
  }
}

@media (max-width: 991px) {
  header {
    padding: 15px;
  }
  header label {
    display: initial;
  }
  header .navbar {
    position: absolute;
    top: 64px;
    left: 0;
    right: 0;
    background: #ffffff;
    display: none;
    z-index: 99999;
  }
  header .navbar ul li {
    width: 100%;
  }
  header .navbar ul li ul {
    position: relative;
    width: 100%;
  }
  header .navbar ul li ul li {
    background: #ffffff
  }
  header .navbar ul li ul li ul {
    width: 100%;
    left: 0;
  }
  #menu-bar:checked~.navbar {
    display: initial
  }
  header .navbar ul li .language {
    width: 100%;
  }
  header .navbar ul a {
    display: block;
    width: 100%;
    height: 100%;
  }
}

HTML代码

<header>
  <img routerLink="" style="cursor: pointer" src="assets/logo.png" alt="">
  <input type="checkbox" id="menu-bar">
  <label for="menu-bar">{{'header.fifth' | translate}}</label>
  <nav class="navbar">
    <ul>
      <li><a routerLink="">{{'header.first' | translate}}</a></li>
      <li style="cursor: pointer;"><a href="javascript:void(0)">{{'header.second' | translate}} </a>
        <ul style="cursor: pointer;">
          <li><a style="cursor: pointer;" routerLink="/customs">{{'body.head-1' | translate}}</a></li>
          <li><a style="cursor: pointer;" routerLink="/terrestrial">{{'body.head-2' | translate}}</a></li>
          <li><a style="cursor: pointer;" routerLink="/ship">{{'body.head-3' | translate}}</a></li>
          <li><a style="cursor: pointer;" routerLink="/train">{{ 'body.head-4' | translate}}</a></li>
        </ul>
      </li>
      <li style="cursor: pointer;"><a href="javascript:void(0)">{{'header.fourth' | translate}}</a>
        <ul class="language" style="float: left !important; cursor: pointer;">
          <li><a (click)="setLang('ka')" style="cursor: pointer" class="dropdown-item">{{'language.first' | translate}}</a></li>
          <li><a (click)="setLang('en')" style="cursor: pointer" class="dropdown-item">{{'language.second' | translate}}</a> </li>
          <li><a (click)="setLang('ru')" style="cursor: pointer" class="dropdown-item">{{'language.third' | translate}}</a> </li>
        </ul>
      </li>
    </ul>
  </nav>
</header>

内容的提问来源于stack exchange,提问作者mchedlo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 07:54:18