【发布时间】:2019-05-14 20:47:53
【问题描述】:
我有一个 div,在里面我根据某些条件渲染 span。如果组件有一些孩子,我希望这个 div 扩展。如果我在内部跨度之外的区域上单击此 div,它工作正常。
一个例子,是下面的Image。单击该行时,它会扩展区域以显示诸如this 之类的项目。但是,当我单击标题文本时,它不起作用,单击事件不会触发。
这是我的代码:
<div className={headerStyles.header} onClick={(e) => this.selectHeader(e, this.props.items.length)}>
{this.props.items.length > 0 &&
<span className={headerStyles.expand} style={{ color: this.props.headerNameColor }}></span>
}
<span style={{ color: this.props.headerNameColor }} >{this.props.headerName}</span>
{this.props.headerUrl &&
<a
style={{ color: this.props.headerNameColor }}
href={this.props.headerUrl}
data-interception="off"
target="_blank"
title="Open link in a new tab">
<i className={['ms-Icon', 'ms-Icon--OpenInNewWindow', headerStyles.openNewTab].join(' ')}></i>
</a>
}
</div>
这是点击标题时调用的函数:
selectHeader(e, numOfItems) {
if (e.target.children.length > 0 && numOfItems > 0) {
e.target.children[0].classList.toggle(headerStyles.expand)
e.target.children[0].classList.toggle(headerStyles.collapse)
e.target.parentElement.querySelector(`.${headerStyles.items}`).classList.toggle(headerStyles.hidden)
}
else if(this.props.headerUrl){
}
}
感谢任何指导。
谢谢
【问题讨论】:
-
函数
this.selectHeader是什么样的?事件目标将根据您单击的内部元素而有所不同,如果您将该事件传递给不同的函数,则需要小心处理。 -
嗨@jered,请查看我更新的帖子。
标签: javascript css reactjs