简体   繁体   中英

How to show/hide only the specific div class from a php foreach loop

I found similar questions here but due to my difficult design structure I am not sure how to implement that here. This is a post and comments like one in facebook. The whole post is created through the foreach loop. So there will be multiple posts with the same div class name. Within the post i have classes namely postbox and commentbox. There is a div class box within the post box to show/hide comments section which i use as a button to click on. So when user clicks on that it should show/hide only the comment section of that particular post. So in nutshell the one in grey is the button and the one in light green is the div class I want to show. The whole thing is in a for each loop.Can someone help me to achieve this through jQuery ? That will be a great help. Thanks in advance. 在此处输入图片说明

Update :- After reading some comments I realized that a unique div id is required So I made changes and added unique ids in the fooreach loop when I create them. I use the following jquery to get the selected id

$(function() {
  $('div').click(function() {
    displayBlock($(this).attr('class'));
    });
});

and I use this function to get the commentbtn_box class name. So I am hoping that using the next sibling of the parent of the commentbtn_box can solve my issue. But I dont know how to execute that though. Can anyone help me ? Thanks for your comments.

You should have the Class name and the index of the class to get specific.

$(document).on('click', '.something', function(){
 $('.className:nth-child(1)').hide();
 $('.className:nth-child(2)').show();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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