这是一个基本的 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>
);
}
}
最佳答案
我认为您可能需要 event.target.value
而不是 event.target.attr('value')
。这将为您提供输入元素中的当前值,如 react docs 中所述。 .
我的猜测是,您实际上得到的是 event.target.attr is not a function
作为错误消息,因为 dom 元素(如 event.target)没有此方法,就像说,一个 jquery 对象会。
为了更清楚一点,我相信这对你有用:
saveBubble(event) {
Bubbles.insert({
text: event.target.value
})
}
关于javascript - React 组件事件处理程序 - 无法访问事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717761/