Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS onClick不工作-浏览器和系统控制台中未发生任何事件_Javascript_Reactjs_Events_Onclick - Fatal编程技术网

Javascript ReactJS onClick不工作-浏览器和系统控制台中未发生任何事件

Javascript ReactJS onClick不工作-浏览器和系统控制台中未发生任何事件,javascript,reactjs,events,onclick,Javascript,Reactjs,Events,Onclick,我已经读了很多关于这个问题的文章,但仍然无法解决它 /** @jsx React.DOM */ var React = require('react/addons'); var SegmentComponent = React.createClass({ handleThatEvent: function (e) { console.log('clicked'); }, render: function () { c

我已经读了很多关于这个问题的文章,但仍然无法解决它

/** @jsx React.DOM */

var React = require('react/addons');

var SegmentComponent = React.createClass({

    handleThatEvent: function (e)
    {
        console.log('clicked');
    },

    render: function ()
    {
        const styles = {
            left: this.props.leftPercent + '%',
            width: this.props.widthPercent + '%'
        };

        if (this.props.color !== undefined)
        {
            styles.backgroundColor = this.props.color;
        }

        return (
            <span onClick={this.handleThatEvent()} style={styles} className="track-notation"></span>
        )
    }
});

module.exports = SegmentComponent;

还是什么都没有

在HTML中,span元素中没有onClick属性:

<span style="left:10%;width:10%;" class="track-notation" data-reactid=".rru3h8zkm2.1:0.0.2.0"></span>

这就是问题所在:

onClick={this.handleThatEvent()}
应该是这样的:

onClick={this.handleThatEvent.bind(this)}

这就是问题所在:

onClick={this.handleThatEvent()}
应该是这样的:

onClick={this.handleThatEvent.bind(this)}

取代

  onClick={this.handleThatEvent} 
取代

  onClick={this.handleThatEvent} 

小心地将
this.handleThatEvent()
传递到
onClick
事件中并在那里执行,只需传递函数
this.handleThatEvent
on
onClick
将执行它。。。见:

const SegmentComponent=React.createClass({
handleThatEvent:函数(e){
console.log('clicked');
},
渲染:函数(){
常量样式={
左:this.props.leftPercent+“%”,
宽度:this.props.widthPercent+“%”
};
if(this.props.color!==未定义){
style.backgroundColor=this.props.color;
}
返回(
点击我!
)
}
});
ReactDOM.render(
,
document.getElementById('root'))
)

小心将
this.handleThatEvent()
传递到
onClick
事件中并在那里执行,只需传递函数
this.handleThatEvent
on
onClick
将执行它。。。见:

const SegmentComponent=React.createClass({
handleThatEvent:函数(e){
console.log('clicked');
},
渲染:函数(){
常量样式={
左:this.props.leftPercent+“%”,
宽度:this.props.widthPercent+“%”
};
if(this.props.color!==未定义){
style.backgroundColor=this.props.color;
}
返回(
点击我!
)
}
});
ReactDOM.render(
,
document.getElementById('root'))
)


React.creatClass不自动binding@Simon你能试试这个吗:onclick='handleThatEvent()'。。不确定。。或onclick='handleThatEvent'。。。或者尝试在其周围添加一个。。。正在使用按钮吗?React.creatClass会自动binding@Simon你能试试这个吗:onclick='handleThatEvent()'。。不确定。。或onclick='handleThatEvent'。。。或者尝试在其周围添加一个。。。用按钮工作吗?它不工作。span元素中没有“onClick”属性。您是否已阅读事件的react文档。onClick是react,它永远不会在span元素中,onClick在DOM中不存在,它不工作。span元素中没有“onClick”属性。您是否已阅读事件的react文档。onClick是react,它永远不会在span元素中,onClick在DOM中不存在。我添加了实时工作示例。。。那么你有不同的problem@Simon也许您不使用Javascript编译器。就像BabelI添加了实时工作示例一样。。。那么你有不同的problem@Simon也许您不使用Javascript编译器。像巴别塔
  onClick={this.handleThatEvent}