下拉菜单链接不起作用

发布于 2025-01-01 21:59:31 字数 449 浏览 0 评论 0原文

我构建了一个简单的列表并向其中添加了 css。现在垂直菜单可以工作了..问题出在 css 部分。列表项区域比链接本身大。这意味着,如果用户单击该区域,则不会发生任何事情,因为链接区域未覆盖所有列表项区域。

#sidebar1 li {
  list-style: none;
  position: relative;
  width: 120px;
  height: 30px;
  padding: 0 20px;
  background-color: black;
  line-height: 30px;
  cursor: pointer;
}

#sidebar1 li a {
  text-decoration: none;
  color: white;    
}

我想做的是将链接填充或宽度与列表宽度相匹配。因此,无论用户何时单击菜单项,都会单击一个链接。这就是问题所在,我尝试过但没有成功

I build a simple list and added to it css. Now the vertical menu works.. the problem is in the section of the css. The list items area is bigger than the links themselves. That means that if the user clicks on the area, nothing happens cause the links area doesnt cover all the lists items area.

#sidebar1 li {
  list-style: none;
  position: relative;
  width: 120px;
  height: 30px;
  padding: 0 20px;
  background-color: black;
  line-height: 30px;
  cursor: pointer;
}

#sidebar1 li a {
  text-decoration: none;
  color: white;    
}

What I thought to do was to match the links padding or width to that of the lists width. So wherever the users clicks on the menu's item a link will be clicked. Thats problem is that i tried it and it didnt work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2025-01-08 21:59:31

将大部分样式移至 A 标签并修复一些问题:

#sidebar1 li{
list-style: none;
position: relative;
margin:0 <-- added
padding:0  <-- added.
}

#sidebar1 li a{
text-decoration:none;
color: white;
width:120px;
height: 30px;
padding:0 20px;
background-color: black;
line-height: 30px;
cursor:pointer;
display:block <-- this is important
}

Move most of the styling to the A-tag and fix a few things:

#sidebar1 li{
list-style: none;
position: relative;
margin:0 <-- added
padding:0  <-- added.
}

#sidebar1 li a{
text-decoration:none;
color: white;
width:120px;
height: 30px;
padding:0 20px;
background-color: black;
line-height: 30px;
cursor:pointer;
display:block <-- this is important
}
星軌x 2025-01-08 21:59:31

只需使用 display: block 使 a 元素填充父元素的可用水平宽度:

#sidebar1 li a{
    text-decoration:none;
    color: white;
    display: block;
    height: 100%;
}

height: 100% 强制 a 元素填充父元素的可用水平宽度: >a 继承父元素的完整高度。删除父 li 中的内边距,否则您将在 ali 的边缘之间强制留出空格。

此外,在您的 li 中,我不仅删除了 padding(这只会导致如上所述的问题),还删除了 cursor:pointer ,就像用户将鼠标悬停在链接上一样,光标会自动更改,如果用户不在链接上,则光标的类型(指针的类型)在单击时不会产生任何效果,这只是令人困惑:

#sidebar1 li {
    list-style: none;
    position: relative;
    width:120px;
    height: 30px;
    background-color: black;
    line-height: 30px;
}

Just use display: block to make the a element fill the available horizontal width of the parent element:

#sidebar1 li a{
    text-decoration:none;
    color: white;
    display: block;
    height: 100%;
}

The height: 100% forces the a to inherit the full height of the parent element. Remove padding from the parent li, otherwise you'll enforce a space between the edges of the a and the li.

Further, in your li I've not only removed the padding (which simply causes problems as noted above), but also the cursor: pointer, as if the user hovers over the link the cursor will change automatically, if they're not over the link then the cursor's type, that of pointer, is merely confusing when clicking produces no effect:

#sidebar1 li {
    list-style: none;
    position: relative;
    width:120px;
    height: 30px;
    background-color: black;
    line-height: 30px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文