我正在渲染年份列表(2017、2018、2019 ..等)
我正在尝试 console.log 年份值
<a href="#" onClick={() => console.log(event.target.getAttribute('data-year'))} data-year={year}>{year}</a>
我已经尝试了 getAttribute 但一直得到“undefined”
event.target.getAttribute('data-year')
https://jsfiddle.net/69z2wepo/83227/
有什么想法吗?
最佳答案
尝试使用数据属性和 event.target.dataset.value
:
class ControlText extends React.Component {
constructor(props){
super(props);
this.state = {
value: ""
};
}
update(event) {
console.log(event);
this.setState({value: event.target.dataset.value});
}
render() {
console.log(this.state);
var value = this.state.value;
return <a type="text" data-value={2017} onClick={this.update.bind(this)} > 2017 </a>
}
}
ReactDOM.render(
<ControlText />,
document.getElementById('container')
);
关于javascript - React - 如何访问数据集值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221273/