这是一个基本的JavaScript问题,但仍然让我搜索了一段时间。基于this article,下面的代码应该工作,但我得到event.target is not a function
错误saveBubble
。当我在调试器中尝试event
时,错误显示为Uncaught: illegal access
。 arguments
array有需要的事件,但为什么当我拨打event
时它不工作?React组件事件处理程序 - 无法访问事件
export default class Bubble extends Component {
saveBubble(event) {
Bubbles.insert({
text: event.target.attr('value') // <- throws an error here
})
}
body() {
const { text } = this.props.bubble;
if (text) {
return text;
}
else {
return (
<input type='text' onBlur={ this.saveBubble.bind(this) }/>
)
}
}
render() {
return (
<div className='bubble-wrapper'>
<div className='body'>
{ this.body() }
</div>
</div>
);
}
}
我的猜测是,'this'在'体()'函数是不是你认为它是。尝试在你的'render()'中绑定它。 – ivarni
@ivarni在'saveBubble'和'body'中,'this'是Bubble对象 –
我也注意到,如果我要求事件,它会给我“未捕获的非法访问”错误。但是,如果我问event.target它似乎工作。 – stealthysnacks