切换/记录/点击并关闭其他?

时间:2017-04-07 07:33:51

标签: javascript jquery html css css3

以下是该方案。

第1步 - 点击操作会打开一个下拉菜单,只需点按下拉区域和操作文字旁边的任意位置即可关闭该下拉菜单。

问题1 - 我希望这也只是点击动作文字。因此,除了下拉区域以及单击操作文本之外,解决方案将是点击区域。 enter image description here

第2步 - 点击多个操作文本后会打开多个下拉列表。

问题2 - 我想要的方式是,如果一个人打开,其他人就会接近。 enter image description here

代码:

$('.ToggleClass a').click(function(ev) {
  $(this).next('.ActionDropDown').fadeIn(500);


});
$(document).click(function(e) {
  e.stopPropagation();
  var container = $(".ToggleClass");

  //check if the clicked area is dropDown or not
  if (container.has(e.target).length === 0) {
    $('.ActionDropDown').fadeOut(500);
  }

});
* {
  margin: 0px;
  padding: 0px;
  float: left;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  font-family: arial;
}

a {
  text-decoration: none;
}

body,
html {
  width: 100%;
  height: 100%;
  background: #cbeeff
}

.row1 {
  width: 100%;
  background: #fff;
  margin: 5px 0;
}

.row1-2 {
  width: 50%;
  position: relative;
  text-align: center;
  padding: 10px 0;
  border-right: 2px solid #000
}

.row1-2+.row1-2 {
  border-right: 0px;
}

.row1-2 a {
  color: #ff4886;
  float: none;
}

.ActionDropDown {
  position: absolute;
  background: #7bd4ff;
  border: 2px solid #000;
  top: 20px;
  padding: 30px 10px;
  display: none;
  z-index: 9;
  right: 0px;
  left: 0px;
  max-width: 180px;
  margin: 0px auto;
}
.InfoDiv{ width:100%; padding:10px; background:#ffadc9;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-----Row 1--------->
<div class="row1">
  <div class="row1-2">Simple Text</div>
  <div class="row1-2 ToggleClass"> <a href="#">Action</a>
    <div class="ActionDropDown">
      I am a Drop Down !
    </div>
  </div>
</div>
<!-----Row 1--------->
<!-----Row 2--------->
<div class="row1">
  <div class="row1-2">Simple Text</div>
  <div class="row1-2 ToggleClass"> <a href="#">Action</a>
    <div class="ActionDropDown">
      I am a Drop Down !
    </div>
  </div>
</div>
<!-----Row 2--------->
<!-----Row 3--------->
<div class="row1">
  <div class="row1-2">Simple Text</div>
  <div class="row1-2 ToggleClass"> <a href="#">Action</a>
    <div class="ActionDropDown">
      I am a Drop Down !
    </div>
  </div>
</div>
<!-----Row 3--------->

<div class="InfoDiv">
 <b>Info:</b> Right now click on Action on each row open drop down multiple times. What I want is when one drop down is open on click of action , the other drop down if open should get close.
</div>

1 个答案:

答案 0 :(得分:1)

请参阅下面的代码段或jsfiddle

将此行添加到您的jQuery:

$(this).parents(".row").siblings(".row").find(".ActionDropDown").fadeOut(500)

还为HTML中的所有父行(类row)添加了一个公共类

您需要搜索其他ActionDropDown并在打开另一个时关闭它

编辑:在容器+解释

之外添加了新的JQ代码

您可以向IF添加新条件

&& $(".ActionDropDown").has(e.target).length === 0) - &gt;如果被点击的元素不是ActionDropDown的后代。只有你需要它

请参阅下面的更新小提琴或片段

EDIT2:添加条件以检查在单击相同操作时是否打开下拉列表。

var dropDown = $(this).next('.ActionDropDown')
if ($(dropDown).is(":visible")) {
  $(dropDown).fadeOut(500)
} else {
 $(dropDown).fadeIn(500)
}

请参阅下面的代码段或更新的小提琴

$('.ToggleClass a').click(function(ev) {
var dropDown = $(this).next('.ActionDropDown')
  if ($(dropDown).is(":visible")) {
	  $(dropDown).fadeOut(500)
	} else {
	 $(dropDown).fadeIn(500)
	}

	$(this).parents(".row").siblings(".row").find(".ActionDropDown:visible").fadeOut(500)


});
$(document).mouseup(function (e)
{

    if (!$(".ActionDropDown").is(e.target) ) // if the target of the click is not the dropDown
   {
        $(".ActionDropDown").fadeOut(500);
    }
});
* {
  margin: 0px;
  padding: 0px;
  float: left;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  font-family: arial;
}

a {
  text-decoration: none;
}

body,
html {
  width: 100%;
  height: 100%;
  background: #cbeeff
}

.row1 {
  width: 100%;
  background: #fff;
  margin: 5px 0;
}

.row1-2 {
  width: 50%;
  position: relative;
  text-align: center;
  padding: 10px 0;
  border-right: 2px solid #000
}

.row1-2+.row1-2 {
  border-right: 0px;
}

.row1-2 a {
  color: #ff4886;
  float: none;
}

.ActionDropDown {
  position: absolute;
  background: #7bd4ff;
  border: 2px solid #000;
  top: 20px;
  padding: 30px 10px;
  display: none;
  z-index: 9;
  right: 0px;
  left: 0px;
  max-width: 180px;
  margin: 0px auto;
}
.InfoDiv{ width:100%; padding:10px; background:#ffadc9;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-----Row 1--------->
<div class="row row1">
  <div class="row1-2">Simple Text</div>
  <div class="row1-2 ToggleClass"> <a href="#">Action</a>
    <div class="ActionDropDown">
      I am a Drop Down !
    </div>
  </div>
</div>
<!-----Row 1--------->
<!-----Row 2--------->
<div class="row row1">
  <div class="row1-2">Simple Text</div>
  <div class="row1-2 ToggleClass"> <a href="#">Action</a>
    <div class="ActionDropDown">
      I am a Drop Down !
    </div>
  </div>
</div>
<!-----Row 2--------->
<!-----Row 3--------->
<div class="row row1">
  <div class="row1-2">Simple Text</div>
  <div class="row1-2 ToggleClass"> <a href="#">Action</a>
    <div class="ActionDropDown">
      I am a Drop Down !
    </div>
  </div>
</div>
<!-----Row 3--------->

<div class="InfoDiv">
 <b>Info:</b> Right now click on Action on each row open drop down multiple times. What I want is when one drop down is open on click of action , the other drop down if open should get close.
</div>

相关问题