[英]Change the styling of a child div with CSS hover on parent JSX
I'm using the material-ui table.我正在使用 material-ui 表。 Within it I am mapping the rows as such:
在其中,我将行映射为:
.map(row => (
<TableRow
className={classes.clickableRow}
key={row[rowKey]}
>
{generateTableCheckbox(row)}
{
columns.map(column => (
<TableCell
onClick={onRowClick && !row.disabled
? event => onRowClick(event, row)
: null}
key={column.field}
className={classes.body}
title={column.alt ? column.alt(row) : row[column.field]}
>
{column.format ? column.format(row) : row[column.field]}
</TableCell>
))
}
{generateTableHoverOptions(row)}
</TableRow>
))
As you can see the className
gets set to clickableRow
.如您所见,
className
被设置为clickableRow
。
The TableRow
contains a hover options div that is rendered & placed to the right with this function: TableRow
包含一个悬停选项 div,该 div 使用此功能呈现并放置在右侧:
const generateTableHoverOptions = () => {
if (selected) {
return (
<TableCell className={classes.rightHoverIcon}>
<Icon>expand_more</Icon>
</TableCell>
);
}
return null;
};
I want to be able to change the backgroundColor
of the <TableCell />
(className being rightHoverIcon
) that is returned from that function, when hovering over the clickableRow
.我希望能够改变
backgroundColor
所述的<TableCell />
类名是rightHoverIcon
),其从该函数返回,悬停在时clickableRow
。 Here is the js CSS I've been attempting to use:这是我一直在尝试使用的 js CSS:
clickableRow: {
'&:hover': {
backgroundColor: theme.palette.background.default,
cursor: 'pointer',
},
'&:hover > .rightHoverIcon': {
backgroundColor: 'red',
},
},
rightHoverIcon: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
marginTop: 1,
height: 49,
position: 'absolute',
right: 0,
borderBottomWidth: 1,
borderBottomColor: 'rgba(224, 224, 224, 1)',
},
The code expecting to target the div is '&:hover > .rightHoverIcon'
, I've tried other variations such as '&:hover ~ .rightHoverIcon'
and &:hover .rightHoverIcon
but none seem to be working.期望以 div 为目标的代码是
'&:hover > .rightHoverIcon'
,我尝试了其他变体,例如'&:hover ~ .rightHoverIcon'
和&:hover .rightHoverIcon
但似乎都没有工作。
I've checked other questions on SO and while similar they're not like my issue.我已经检查了关于 SO 的其他问题,虽然相似,但它们不像我的问题。 If anyone has any ideas please let me know!
如果有人有任何想法,请告诉我!
Thanks in advance提前致谢
&:hover > .rightHoverIcon
expects .rightHoverIcon
to be a direct child of clickableRow
, but it isn't (there's a table cell in-between). &:hover > .rightHoverIcon
期望.rightHoverIcon
是clickableRow
的直接子.rightHoverIcon
,但它不是(中间有一个表格单元格)。 Remove the >
so you're using a descendant combinator instead of a child combinator :删除
>
以便您使用 后代组合器而不是子组合器:
clickableRow: {
'&:hover': {
backgroundColor: theme.palette.background.default,
cursor: 'pointer',
},
'&:hover .rightHoverIcon': {
// -----^
backgroundColor: 'red',
},
},
Or, alternately, if you want to use child combinators, you'll need at least two of them (assuming TableCell
only puts one element between its parent and the icon), eg &:hover > something-for-the-cell > .rightHoverIcon
.或者,如果你想使用子组合器,你至少需要两个(假设
TableCell
只在它的父元素和图标之间放置一个元素),例如&:hover > something-for-the-cell > .rightHoverIcon
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.