【发布时间】:2016-06-09 05:45:25
【问题描述】:
这是一个基本的 JavaScript 问题,但还是让我在谷歌上搜索了一段时间。基于this article,下面的代码应该可以工作,但是我在saveBubble 中得到event.target is not a function 错误。当我在调试器中尝试event 时,错误显示为Uncaught: illegal access。 arguments数组有需要的事件,但是为什么我调用event时它不起作用?
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>
);
}
}
【问题讨论】:
-
我的猜测是
body()函数中的this并不是你想象的那样。尝试将其绑定到您的render()。 -
@ivarni 在
saveBubble和body中,this是 Bubble 对象 -
我还注意到,如果我要求事件,它会给我“未捕获的非法访问”错误。但如果我要求 event.target 它似乎工作。
标签: javascript mongodb meteor reactjs event-handling