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

问题描述 投票:1回答:1

我有一个名为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>[email protected]</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"});
        }
    }
});
javascript jquery html css jquery-selectors
1个回答
0
投票

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

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

当你使用mouseup并检查e.target事件传播时,这里也不需要!

下面给出的片段:

/** 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()
});
<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>[email protected]</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.