热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在图像悬停时更改div的属性-changepropertyofdivonhoverofanimage

#myUserMenu{position:absolute;background-color:white;top:20px;width:116px;

#myUserMenu {
  position: absolute;
  background-color: white;
  top: 20px;
  width: 116px;
  border: 1px solid #CCC;
}
#myAvatar:hover #myUserMenu {
  background-color: red;
}
.menuItem {
  cursor: pointer;
  border-bottom: 1px solid #EEE;
}
some text here...

So when I hover the myAvatar, myUserMenu background should change to red

所以当我悬停myAvatar时,myUserMenu背景应该变为红色

#myAvatar:hover #myUserMenu

And nothing happens ! Any idea why ?

没有任何反应!知道为什么吗?

4 个解决方案

#1


2  

Enclose the text inside a span and use + operator to affect the next element's style.

将文本括在span中并使用+运算符来影响下一个元素的样式。

#myUserMenu {
  position: absolute;
  background-color: white;
  top: 20px;
  width: 116px;
  border: 1px solid #CCC;
}
#myAvatar:hover + #myUserMenu {
  background-color: red;
}
.menuItem {
  cursor: pointer;
  border-bottom: 1px solid #EEE;
}
some text here...

#2


1  

#myAvatar:hover #myUserMenu {
    background-color: red;
}

This selector is looking for #myUserMenu inside #myAvatar. Obviously that won't work because it's outside #myUserMenu.

此选择器正在#myAvatar中查找#myUserMenu。显然这不起作用,因为它在#myUserMenu之外。

What you could do is look for #myUserMenu immediately after #myAvatar, like so:

您可以做的是在#myAvatar之后立即查找#myUserMenu,如下所示:

#myAvatar:hover + #myUserMenu {
    background-color: red;
}

This is the Adjacent Sibling Combinator. See this article for more details.

这是Adjacent Sibling Combinator。有关详细信息,请参阅此文章。

Or you could rearrange your HTML to put #myUserMenu inside #myAvatar.

或者您可以重新排列HTML以将#myUserMenu放在#myAvatar中。

#3


0  

Your div #myUserMenu is not a child of your image, so you need to target the div with a brother selector :

您的div #myUserMenu不是您图像的子项,因此您需要使用兄弟选择器来定位div:

#myAvatar:hover ~ #myUserMenu {
    background-color:red;
}

JsFiddle: http://jsfiddle.net/ghorg12110/o0c2hppv/

The general sibling combinator selector ~ allow you to select the element which can appear anywhere after it.

通用兄弟组合选择器〜允许您选择可以出现在其后任何位置的元素。

#4


-1  

You have to apply parent child relationship in order to apply hover.

您必须应用父子关系才能应用悬停。

Like this

#myUserMenu {
  position: absolute;
  background-color: white;
  top: 20px;
  width: 116px;
  border: 1px solid #CCC;  
}
#myAvatar:hover #myUserMenu {
    background-color:red;
}

.menuItem {
    cursor:pointer;
    border-bottom:1px solid #EEE;
}
some text here...


推荐阅读
author-avatar
拍友2502920603
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有