简体   繁体   English

单击多个 Div 时显示和隐藏无法正常工作

[英]Show and Hide on Click on multiple Div's not working properly

Please help, I have this code but the hide button does not work请帮忙,我有这个代码,但隐藏按钮不起作用

 $('.expandArrow, .hideArrow').on('click', function() { var isExpand = $(this).hasClass('expandArrow'); $(this).closest('.trDest').toggleClass('visible-description', isExpand).next().toggle(isExpand); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="trDest1" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div> <div id="trDest2" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div> <div id="trDest3" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div>

I just don't get it why it does not work, thanks我只是不明白为什么它不起作用,谢谢

here is the FIDDLE这是小提琴

Use if statement for hasClass line.对 hasClass 行使用 if 语句。 If the element has class it returns true, you are setting the variable with a boolean and not classname如果元素具有类,则返回 true,则使用布尔值而不是类名设置变量

 $('.expandArrow, .hideArrow').on('click', function(){ if($(this).hasClass('hideArrow')); $(this).closest('.trDest').toggleClass('visible-description', 'hideArrow').next().toggle('hideArrow'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="trDest1" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div> <div id="trDest2" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div> <div id="trDest3" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div>

There are two problems in your code...你的代码有两个问题......

First problem lies in your HTML structure or your JS.第一个问题在于您的 HTML 结构或您的 JS。

$('.expandArrow, .hideArrow').on('click', function(){
   if($(this).hasClass('hideArrow'));
    $(this).closest('.trDest').toggleClass('visible-description', 'hideArrow').next().toggle('hideArrow');
});

You add your function to both arrows.您将您的功能添加到两个箭头。 And in that callback, you're looking for the closest .trDest element to clicked arrow.在该回调中,您正在寻找最接近点击箭头的.trDest元素。

So if the clicked arrow is the .expandArrow , then that element is found:因此,如果单击的箭头是.expandArrow ,则找到该元素:

<div id="trDest1" class="trDest">
    <!-- Some content here -->
    <button class="expandArrow">Show</button>
</div>

But if the clicked element is .hideArrow , then there is no closest element with such class and the rest of your code doesn't do anything:但是如果被点击的元素是.hideArrow ,那么就没有最接近此类的元素,其余的代码不会做任何事情:

<div id="trDest2_details" class="details">
    <p>show details</p>
    <button class="hideArrow">Hide</button>    
</div>

Second one is in the part that toggles the class第二个是在切换类的部分

toggleClass can take two parameters: toggleClass可以接受两个参数:

.toggleClass( className, state )

but you pass 'hideArrow' (a string) as a state.但是您将'hideArrow' (一个字符串)作为状态传递。 So it won't be false...所以不会是假的...

So how to do it properly?那么如何正确操作呢?

This should do the trick:这应该可以解决问题:

$('.expandArrow').on('click', function () {
    $(this).closest('.trDest').addClass('visible-description').next().show();
});
$('.hideArrow').on('click', function () {
    $(this).closest('.details').hide().prev().removeClass('visible-description');
});

And even better?甚至更好?

The code above will work, but... I wouldn't use it, because the HTML structure isn't very semantic.上面的代码可以工作,但是...我不会使用它,因为 HTML 结构不是很语义化。 You have two independent HTML tags, that describe one object.您有两个独立的 HTML 标签,用于描述一个对象。 And you set classes on these tags to describe the state of that object.然后在这些标签上设置类来描述该对象的状态。

My approach would be different.我的方法会有所不同。 I would go for something like this:我会去做这样的事情:

<div class="trDest">
  <div class="header">
    <!-- Some content here -->
    <button class="expandArrow">Show</button>
  </div>
  <div class="details">
    <p>Some details...</p>
    <button class="hideArrow">Hide</button>
  </div>
</div>

And toggle the state class on the wrapping element.并在包装元素上切换状态类。

$('.expandArrow, .hideArrow').on('click', function () {
    $(this).closest('.trDest').toggleClass('visible-description', $(this).hasClass('hideArrow'));
});

This way you have one tag for one object and there is a class that is describing its state.这样你就有了一个对象的标签,并且有一个描述它的状态的类。 And as you can see, the JS code is much simpler in such approach.正如你所看到的,这种方法的 JS 代码要简单得多。

You can try below code -您可以尝试以下代码-

 $('.expandArrow, .hideArrow').on('click', function() { if ($(this).hasClass('expandArrow')) { $(this).parent().next().toggleClass('visible-description'); } else { $(this).parent().toggleClass('visible-description'); } });
 .details { display: none; } .visible-description.details { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="trDest1" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div> <div id="trDest2" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div> <div id="trDest3" class="trDest"> <!-- Some content here --> <button class="expandArrow">Show</button> </div> <div id="trDest2_details" class="details"> <p>show details</p> <button class="hideArrow">Hide</button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM