css - 如何在material-ui中的:last-child中使用:hover?
问题描述
我将 React 与 material-ui 一起使用,并且有一个带有列表的菜单组件,此菜单中的最后一项与其他菜单不同,我使用 :last-child 并且它是功能性的,问题是当我尝试使用 :hover 时在最后一个元素中。
风格:
const useStyles = makeStyles((theme) => ({
menu: {
textDecoration: 'none',
color: theme.palette.navy.default,
cursor: 'pointer',
fontWeight: 'bold',
textTransform: 'uppercase',
'&:last-child': {
background: theme.palette.orange.default,
borderRadius: '5em',
color: theme.palette.white.default,
// using hover in the last child item not work
'&:hover': {
background: theme.palette.green.default,
},
},
},
}));
零件:
...
const classes = useStyles();
<MenuList>
{menu.map((item, index) => (
<MenuItem selected={false} key={index} className={classes.menu}>
{item.title}
</MenuItem>
))}
</MenuList>
你知道怎么解决吗?
解决方案
我创建了一个新组件,我在其中重新测试了那些不起作用的选项。我包含了@Sanish Joseph 以前不起作用的答案,这次使用这个新组件,它起作用了。
在上下文中,我创建了一个菜单映射对象数组,每个对象都有另一个带有子菜单的对象数组。在发现问题之前,我一直在丢弃元素。在我用来显示子菜单的 Popper 组件中,有一个名为“disablePortal”的属性。此属性导致“最后一个孩子”中的“悬停”不起作用。
推荐阅读
- jquery - jQuery自动选择选择选项问题时,当不可用的选项
- html - 在 Django 中显示来自 qrcode 的 SVG 图像
- linux - Linux OpenSuse42.3 - 端口状态 - 过滤
- mysql - 具有两个表的 SQL 查询,需要在其中一个表中搜索特定和多列值
- javascript - 使用face api js在脸上上传蒙版,我找到了面部标志并将蒙版附加在上面,但是如何将附加的图像作为一个整体下载?
- python - 在 Tkinter 中添加滚动条,以在框架外显示图像
- php - 使用 preg_replace 或 preg_match_all 函数替换字符串
- r - 在 R 函数中以不规则间隔对数据进行子集化
- reactjs - 你如何一起使用 Framer Motion + Styled Components?
- css - 如何让 vuetify 排版课程正常工作?