【问题标题】:Allow onclick event on TR element but prevent the event in the checkbox允许 TR 元素上的 onclick 事件,但阻止复选框中的事件
【发布时间】:2017-05-13 15:05:14
【问题描述】:

如何允许 TR 元素上的 onClick 事件但阻止复选框中的事件? 复选框是表中 TR 的 TD 的子级。

我在这个问题中所做的是在所有 TD 上添加 onClick,但不在复选框的 TD 中。

有没有更好的方法来解决这个问题?

<tr key={item._id} id={item._id} className={(item.status == "unread")? "unread":""}>
    <td className="inbox-small-cells">
        <input type="checkbox" className="mail-checkbox" />
    </td>
    <td className="view-message  dont-show" onClick={this.viewMsg.bind(this,item.sender)}>{item.sender} {(item.category.length > 0)? 
                        <span className="label label-info pull-right">{item.category}</span>:""}</td>
    <td className="view-message " onClick={this.viewMsg.bind(this,item.sender)}>{item.subject.trunc(40)}</td>
    <td className="view-message  inbox-small-cells" onClick={this.viewMsg.bind(this,item.sender)}>{(item.attachments.length > 0) ? <i className="fa fa-paperclip"></i> : ""}</td>
    <td className="view-message  text-right" onClick={this.viewMsg.bind(this,item.sender)}>{item.createdAt}</td>
</tr>

【问题讨论】:

  • 你的代码在哪里?请附上代码
  • 请输入小代码sn-p..
  • 如果你在调用 js 函数 onClick 你可以考虑在点击复选框时删除这个函数
  • 如果不能禁止传播,可以查看nodeName:event.target.nodeName === 'TR'
  • className=" 这是来自什么类型的属性?

标签: javascript jquery html checkbox jsx


【解决方案1】:

就像大家已经说过的event.stopPropagation()。我们也可以使用 CSS 属性pointer-events:none 或属性disabled:true。以下片段有 2 个使用 .css().attr() 的工作示例

片段

// Option #1
$('.mail-checkbox1').css('pointer-events', 'none');


// Option #2
$('.mail-checkbox2').attr('disabled', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <div class="inbox-small-cells">
    <label>Control #0 (Normal test case)</label>
    <input type="checkbox" class="mail-checkbox0">
    <br/>

    <label>Option #1 (pointer-events:none)</label>
    <input type="checkbox" class="mail-checkbox1">
    <br/>


    <label>Option #2 (disabled: true)</label>
    <input type="checkbox" class="mail-checkbox2">
    <br/>
  </div>
</section>

【讨论】:

    【解决方案2】:

    您可以使用event.stopPropagation() 调用 tds/checkboxes 将事件传播到 dom 树中的父级。

    改成这样:

    <tr key={item._id} id={item._id} onClick={this.viewMsg.bind(this,item.sender)} className={(item.status == "unread")? "unread":""}>
        <td className="inbox-small-cells" onClick={this.stopEventProp(event)}>
            <input type="checkbox" className="mail-checkbox" />
        </td>
        <td className="view-message  dont-show">{item.sender} {(item.category.length > 0)? 
                            <span className="label label-info pull-right">{item.category}</span>:""}</td>
        <td className="view-message ">{item.subject.trunc(40)}</td>
        <td className="view-message  inbox-small-cells">{(item.attachments.length > 0) ? <i className="fa fa-paperclip"></i> : ""}</td>
        <td className="view-message  text-right">{item.createdAt}</td>
    </tr>
    

    现在你必须把这个方法放到你的类中:

    stopEventProp(e){
      e.stopPropagation();
    }
    

    【讨论】:

    • 你能举例说明如何使用它吗?
    • @JohnArellano 最好将您尝试过的代码放入其中。这将有助于我们为您提供更好的解决方案。
    • _this3.stopEventProp 不是函数。这就是结果。
    • 对不起。我放了 stopEventProp 函数,但还是不行。
    • “事件”从何而来?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 2012-10-25
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多