作为React中的新手,我面临一个问题:组件返回一个简单的嵌套表,因此“大表-id”的每个单元格都包含另一个小表“小表-id”。
问题是,每当mouseover事件发生时,我总是以“target.id”的形式获得“小单元格-*”,即使事件处理程序是在父表中引用的。有没有办法让父表有点“不透明”,这样我就可以接收“大表-单元-1”或“小表-id”?
(using Rails with 'react-rails' gem)
var Tables = React.createClass({
handleMouseOver: function(e){
console.log(e.target.id)
},
render: function(){
return (
<table id='big-table-id' onMouseOver={this.handleMouseOver}>
<tr>
<td id='big-table-cell-1'>
<table id='small-table-id'>
<tr>
<td id='small-cell-1>
text 1
</td>
<td id='small-cell-2'>
text 2
</td>
</tr>
</table>
</td>
</tr>
</table>
)
}
});
发布于 2016-12-19 20:26:54
DOM允许您使用firstChild
和parentElement
等方法来选择元素、子节点和父节点。你应该调查一下这些。
编辑:也不确定这是否有效,但您可以尝试将大表包装在div中,并在那里设置回调并查看它引用的内容。
https://stackoverflow.com/questions/41230382
复制相似问题