【问题标题】:jquery/jqueryui, click anywhere in div besides buttonjquery/jqueryui,单击除按钮之外的 div 中的任意位置
【发布时间】:2010-12-11 01:52:40
【问题描述】:

我有一个 div 作为 ui-widget-content 和一个 h3 作为 ui-widget-header。标题有三个按钮。每个都有“保存”“删除”和“取消”类。当您单击 div 时,我希望有一个 .click 功能,除非您实际单击其中一个按钮。

我试过了:$(".record").not(".save").not(".delete").not(".cancel").click(

我的代码是:

<div class="record cursorPointer hoverClass ui-widget-content">
   <h3 class="ui-widget-header">
       <table width="100%"><tr>
           <td align="left" style="width:33%; color:Red">somecontractorone</td>
           <td style="width:33%" align="center"> Name: Joe Q Shmoe </td>
           <td style="width:33%" align="right"> Buisness: joeqshmoeelectrical</td>
           <td><button style="width:20px;height:20px" class="save"></button></td>
           <td><button style="width:20px;height:20px" class="delete"></button></td>
           <td><button style="width:20px;height:20px" class="cancel"></button></td></tr>
       </table>
   </h3>
</div>

但是点击按钮时点击功能仍然激活。

有什么想法吗?

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    您可以使用event.target 查看点击了什么。然后依靠甚至冒泡并将点击处理程序附加到父divjsfiddle

    $('.record').click(function(e){
        if($(e.target).is(':button'))
            alert('button');   
        else
            alert('not button');
    });
    

    【讨论】:

    • '$(".record").click(function(e) { if (!$(e.target).is(':button')) {' 有效。但我不确定它是否有效,我做的方式不...
    • @kralco626 - 如果您仍有问题,请尝试 jsfiddle 示例。
    • 我让它与 e.target 一起工作。只是不知道为什么它不按我的方式工作,但没关系。谢谢!
    • 很好的解决方案,正是我所需要的。谢谢!
    • 乐于帮助@IgorAugusto
    【解决方案2】:

    假设您有另一个(或多个)事件处理程序来处理按钮,您可以通过

    $('button').click(function(e) {
        e.stopPropagation();
        switch( this.className ) {
            // Or whatever...
        }
    })
    

    【讨论】:

      【解决方案3】:

      在按钮的点击处理程序中使用e.stopPropagation()

      $('button').click(function(e) {
          e.stopPropagation();
          alert('you clicked a button!');
      });
      

      这是一个演示:http://jsfiddle.net/Ender/2yERz/

      还有一些关于e.stopPropagation()的文档:http://www.quirksmode.org/js/events_order.html#link9

      【讨论】:

      • 我现在可能会使用 Josiah 的解决方案,直到我真正去实现按钮点击。不过这是个好主意。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2014-09-07
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多