【问题标题】:presence of className preventing onClick handler being calledclassName 的存在阻止 onClick 处理程序被调用
【发布时间】:2015-12-11 01:23:29
【问题描述】:

我有一个简单的 React 组件:

class FilterCheckBox extends React.Component {

  clickCheckBox(e) {
    console.log('clicked on checkbox');
  }

  render(){

    return (
      <div>
        <a className="filter-checkbox" onClick={this.clickCheckBox.bind(this)}>
          <span>{this.props.area.key}</span>
          <span>{this.props.area.doc_count}</span>
        </a>
      </div>
    )
  }
}

使用上面指定的 className,永远不会调用 onClick 处理程序。但是,如果我删除 className 声明,onClick 工作正常。

类添加的样式有:

.filter-checkbox,
.filter-checkbox:link,
.filter-checkbox:visited,
.filter-checkbox:focus {
  -moz-transition: color 0.5s ease;
  -o-transition: color 0.5s ease;
  -webkit-transition: color 0.5s ease;
  transition: color 0.5s ease;
  color: #546e7a;
  text-transform: uppercase;
  text-decoration: none;
  font: normal normal 700 0.875em/1.2em "Oxygen", sans-serif;
  /*14px*/
  outline: none;
  position: relative;
  display: block;
  margin-bottom: 10px;
}
/* line 330, ../scss/partials/_reusables.scss */
.filter-checkbox:before,
.filter-checkbox:link:before,
.filter-checkbox:visited:before,
.filter-checkbox:focus:before {
  -moz-transition: all 0.8s ease;
  -o-transition: all 0.8s ease;
  -webkit-transition: all 0.8s ease;
  transition: all 0.8s ease;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  content: '';
  background: #ffffff;
  border: 1px solid #cfd8dc;
  color: #007cba;
  clear: none;
  cursor: pointer;
  display: inline-block;
  height: 15px;
  line-height: 0;
  width: 15px;
  outline: none;
  vertical-align: middle;
  margin: -3px 10px 0 0;
}
/* line 347, ../scss/partials/_reusables.scss */
.filter-checkbox:after,
.filter-checkbox:link:after,
.filter-checkbox:visited:after,
.filter-checkbox:focus:after {
  display: none;
  content: '\f00c';
  color: #ffffff;
  font: normal normal 400 11px/11px FontAwesome;
  position: absolute;
  top: 2px;
  left: 2px;
}
/* line 356, ../scss/partials/_reusables.scss */
.filter-checkbox:hover,
.filter-checkbox:link:hover,
.filter-checkbox:visited:hover,
.filter-checkbox:focus:hover {
  color: #50d583;
  text-decoration: none;
}
/* line 361, ../scss/partials/_reusables.scss */
.filter-checkbox.active:before,
.filter-checkbox:link.active:before,
.filter-checkbox:visited.active:before,
.filter-checkbox:focus.active:before {
  border: 1px solid #50d583;
  background: #50d583;
}
/* line 365, ../scss/partials/_reusables.scss */
.filter-checkbox.active:after,
.filter-checkbox:link.active:after,
.filter-checkbox:visited.active:after,
.filter-checkbox:focus.active:after {
  display: block;
}

我在这里看不到任何可以阻止事件传播的东西,所以一定有一些我不能正确理解的东西..?

【问题讨论】:

  • @AndreiGheorghiu - 嗯,很确定这就是 标签中的内容。
  • @cimmanon - 用 css 替换了 sass 代码。我真的不明白 css 如何影响事件传播,但我不是 css 专家
  • 我已经尝试注释掉所有相关的 css 规则,但仍然没有调用 onClick。似乎与 React 相关,并且与样式没有任何关系。
  • 感谢您的 cmets/建议 - 我终于发现还有另一个我不知道的脚本正在捕获点击事件...grrr
  • @PeterWhitfield 很高兴您发现了问题。你介意回答这个问题吗?

标签: javascript css reactjs


【解决方案1】:

事实证明,这是由另一个脚本引起的,该脚本使用 jquery 来捕获单击事件以定位特定的类名。

通过消除过程找到 - 首先消除所有脚本并恢复,直到问题再次出现。然后检查问题脚本,直到找到有问题的行。

感谢您的回复。

【讨论】:

  • Chrome devtools 允许您检查附加到元素的事件侦听器,可以考虑使用它来调试为什么侦听器将来不会触发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2012-11-18
  • 1970-01-01
相关资源
最近更新 更多