• onclick和mousedown冲突怎么办???


    js代码:
    $(function() { $(document).mousedown(function(event) { if (event.target.id == 'color') { document.getElementById("x-palette-panel").style.display = 'block'; } else { document.getElementById("x-palette-panel").style.display = 'none'; } }); });

    html代码:

    <div id="x-palette-panel" class="panel" style="left: 500px; display: none;">
       <span style="color:#000000;background-color:#ffffff;" onclick="highContrast(0)">黑底白字</span>
    </div>
    

    我通过点击某个图片使<div id="x-palette-panel">显示出来,点击页面除这个图片外的其他地方这个
    <div id="x-palette-panel">隐藏。。。

    我的问题就是当我在<div id="x-palette-panel">触发onclick时先触发的是鼠标点击事件引起div又隐藏,这样就不触发
    onclick的方法。。。

    别人的解决方法:

    方法1、highContrast里面禁止事件冒泡

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title>无标题页</title>
      <script type="text/javascript" src="jquery-1.8.1.min.js"></script>
    </head>
    <body>
      <form id="form1" runat="server">
      <div id="x-palette-panel" class="panel" style="left: 500px; display: none;">
        <span style="color: #000000; background-color: #ffffff;" onclick="highContrast(event,0)">
          黑底白字</span>
      </div>
      <img src="http://avatar.profile.csdn.net/C/9/4/2_net_lover.jpg" id="color" />
      <div>其他地方的内容</div>
      </form>
      <script type="text/javascript">
        $(function () {
          $(document).click(function (event) {
            if (event.target.id == 'color') {
              document.getElementById("x-palette-panel").style.display = 'block';
            }
            else {
              document.getElementById("x-palette-panel").style.display = 'none';
            }
          });
        });
        function highContrast(e, x) {
          alert("你点击了 黑底白字 参数=" + x);
          if (e && e.stopPropagation) {//非IE  
            e.stopPropagation();
          }
          else {//IE  
            window.event.cancelBubble = true;
          }   
        }
      </script>
    </body>
    </html>
    

     方法2、

     $(function() {
                $(document).mousedown(function(event) {
                    if (event.target.id == 'color') {
                        document.getElementById("x-palette-panel").style.display = 'block';
                    }
                    else if(event.target.id!='x-palette-panel'){ //再做一层判断
                        document.getElementById("x-palette-panel").style.display = 'none';
                    }
                });
    
            });
    
  • 相关阅读:
    卷积层中的特征冗余
    【跨模态智能分析】人物关系检测、指代表达、指代分割
    【第1周作业】“乘风破浪的程序员小哥哥小姐姐” 成团时刻
    2020年秋季《软件工程》开课啦
    初入科研领域,如何正确做科研
    【WACV2020】ULSAM: Ultra-Lightweight Subspace Attention Module
    【ECCV2020】 Context-Gated Convolution
    【ECCV2020】WeightNet: Revisiting the Design Space of Weight Networks
    【ECCV2020】Image Inpainting via a Mutual Encoder-Decoder with Feature Equalizations
    【新生学习】课程学习记录
  • 原文地址:https://www.cnblogs.com/wenghaowen/p/2697024.html
Copyright © 2020-2023  润新知