【发布时间】:2023-03-08 12:00:02
【问题描述】:
我有一个名为 Row 的组件,它当前正在使用 text-overflow: ellipsis 以避免行太长时换行。
这是我目前所拥有的:
function Row(props) {
const { show=true, children } = props;
const style = {
display: show ? 'block' : 'none',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis'
};
const title = React.Children
.map(children, c => c.toString()) // wat do?
.join(' ');
return (<div style={style} title={title}>{children}</div>);
}
const el = document.querySelector('#content');
const component = (<div style={{width:'200px'}}>
<Row>This is the first one works and is very long.</Row>
<Row>The second one works too.</Row>
<Row>But <b>bold</b> children do <b>not</b> work properly.</Row>
<Row show={false}>A hidden row.</Row>
</div>);
ReactDOM.render(component, el);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script>
<h1>Hover over the rows below</h1>
<div id="content"></div>
title 属性可以处理我的工具提示,但是当子节点不是文本节点时,我不知道如何获取它们的内容。
我的下一个想法是,我需要等到孩子被渲染到 DOM,然后添加一个 mouseover 事件来检索文本节点……但这似乎有点过头了。
如何获取子节点的内容作为文本节点并显示为工具提示?
【问题讨论】: