javascript 反应 onClick 不触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30594773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React onClick not firing
提问by Megh Parikh
The following code doesnot work. The onClick events are never fired but I see no errors in browserify or console.
以下代码不起作用。onClick 事件永远不会被触发,但我在 browserify 或控制台中没有看到任何错误。
var React = require('react');
var Button = React.createClass({
render: function() {
return (
<div className="Button">
<span className="left" onclick={function() {alert("left")}}>Left</span>
<span className="right" onclick={function() {alert("right")}}>Right</span>
<span className="middle" onclick={function() {alert("middle")}}>Middle</span>
</div>
);
}
});
module.exports=Button;
I use alert just for testing small CSS
我使用警报只是为了测试小 CSS
回答by Jerome WAGNER
You have a typo in your example. Use 'onClick' instead of 'onclick'.
你的例子中有一个错字。使用“onClick”而不是“onclick”。
<span className="left" onClick={function() {alert("left")}}>Left</span>
see jsfiddle for a working example - https://jsfiddle.net/Ltdc8qpr/1/
有关工作示例,请参阅 jsfiddle - https://jsfiddle.net/Ltdc8qpr/1/
回答by Anupam Maurya
Here with arrow function syntax, triggering the event with onClick handler.
这里使用箭头函数语法,使用 onClick 处理程序触发事件。
<span className="left" onClick={() => {alert("left")}}>Left</span>