简体   繁体   English

反应 onClick 不触发

[英]React onClick not firing

The following code doesn't 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。 Why isn't onclick firing?为什么不点击触发?

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/

这里使用箭头函数语法,使用 onClick 处理程序触发事件。

<span className="left" onClick={() => {alert("left")}}>Left</span>

I was facing the same issue.我面临着同样的问题。 The problem was that the <span> tag was in the top-left corner and the alert would only fire when I would click the top-left corner.问题是<span>标签位于左上角,只有当我单击左上角时才会触发警报。 Not any other space.不是任何其他空间。

I changed <span> to <div> and it worked.我将<span>更改为<div>并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM