关闭按钮在div中的jQuery中不起作用

时间:2016-08-02 18:01:07

标签: javascript jquery html css jquery-selectors

我有一个名为 datahover 的Div,其中我放了jquery,这样如果我点击那个Div内部然后它就不会隐藏但是当我点击那个Div之外然后它隐藏了。但问题是关闭按钮位于该div内部,所以当我点击该关闭按钮时,Div没有隐藏。

此处关闭按钮是 标记,其中包含 I 标记。

HTML代码:

<div class="datahover">                                                    
    <a href="javascript:void(0);"><i class="fa fa-times" aria-hidden="true"></i></a>
    <div class="facultydata">
        <div class="leftside">
            <img src="deepak.jpg">
            <h3>Deepak Chaudhary</h3>
        </div>
        <div class="table-responsive";          
            <table class="table">
                <tbody>
                   <tr>   
                        <th>Designation</td> 
                        <td>ME</td>
                    </tr>
                    <tr>
                        <th>Qualification</td>
                        <td>CE</td>
                    </tr>
                    <tr>
                        <th>Teaching Experience</td>
                        <td>8 Years</td>
                    </tr>
                    <tr>
                        <th>Industry Experience</td>
                        <td>7 Years</td>
                    </tr>
                    <tr> 
                        <th>Department</td>
                        <td>Computer</td>
                    </tr>
                    <tr>
                        <th>Area</td>
                        <td>All</td>
                    </tr>
                    <tr>
                        <th>Email</td>
                        <td>dac81048@gmail.com</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

jquery代码:

/** datahover hide on click outside **/
$(document).mouseup(function (e)
{
    var container = $(".datahover");

    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        container.css({"display": "none"});
        $('.facultysection').css({"background": "#fff"});
        if ($(window).width() > 767) {
            $('html, body').css({"overflow-x": "hidden", "overflow-y": "auto"});
        }
        else
        {
            $('html, body').css({"overflow-x": "hidden", "overflow-y": "auto"});
        }
    }
});

1 个答案:

答案 0 :(得分:0)

我们的想法是在关闭按钮上使用点击事件的stopPropagation,如:

$('.fa-times').click(function(event) {
  event.stopPropagation();
  $(".datahover").hide()
});

当您使用mouseup时,此处也不需要检查e.target事件传播!

以下代码段:

&#13;
&#13;
/** datahover hide on click outside **/
$(document).mouseup(function(e) {
  var container = $(".datahover");

  if (!container.is(e.target) && container.has(e.target).length === 0) {
    container.css({
      "display": "none"
    });
    $('.facultysection').css({
      "background": "#fff"
    });
    if ($(window).width() > 767) {
      $('html, body').css({
        "overflow-x": "hidden",
        "overflow-y": "auto"
      });
    } else {
      $('html, body').css({
        "overflow-x": "hidden",
        "overflow-y": "auto"
      });
    }
  }
});

$('.fa-times').click(function(event) {
  event.stopPropagation();
  $(".datahover").hide()
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<div class="datahover">
  <a href="javascript:void(0);"><i class="fa fa-times" aria-hidden="true"></i></a>
  <div class="facultydata">
    <div class="leftside">
      <img src="deepak.jpg">
      <h3>Deepak Chaudhary</h3>
    </div>
    <div class="table-responsive">
      <table class="table">
        <tbody>
          <tr>
            <td>Designation</td>
            <td>ME</td>
          </tr>
          <tr>
            <td>Qualification</td>
            <td>CE</td>
          </tr>
          <tr>
            <td>Teaching Experience</td>
            <td>8 Years</td>
          </tr>
          <tr>
            <td>Industry Experience</td>
            <td>7 Years</td>
          </tr>
          <tr>
            <td>Department</td>
            <td>Computer</td>
          </tr>
          <tr>
            <td>Area</td>
            <td>All</td>
          </tr>
          <tr>
            <td>Email</td>
            <td>dac81048@gmail.com</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;