這是一個基本的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