简体   繁体   English

如何通过单击子ID找到父ID,然后通过jQuery获取父ID来找到子ID

[英]How to find parent id by click on child id and then child id by getting parent id via jquery

I am creating commenting module on product page in which i am showing option of spam comment and there i am showing onclick show and hide popup. 我在产品页面上创建评论模块,在该页面中,我显示垃圾邮件评论选项,并且在其中显示onclick显示和隐藏弹出窗口。 now it is working fine in single comment but when comment is two it opens both on one time because the class is same. 现在它在单个注释中运行良好,但是当注释为两个时,由于类相同,因此一次打开两个注释。 Now that's why i want to get the parent li id so we can find only the internal class of that id not the another one 这就是为什么我要获取父级li id的原因,因此我们只能找到该id的内部类,而不能找到另一个

Here is my jsfiddle link 这是我的jsfiddle链接

this is my script 这是我的剧本

$(".slidermenudown .fa-angle-down").on("click",function(){

 if($(".anthr_usr_cmnt").hasClass("active")){

    $(".anthr_usr_cmnt").removeClass("active");

  }else{
    if($(".owner_cmnt").hasClass("active")){
      $(".owner_cmnt").removeClass("active");
    }
    $(".anthr_usr_cmnt").addClass("active");
  }

});

Try this: 尝试这个:

JS JS

$(".slidermenudown .fa-angle-down").on("click",function(){
    var parentli = $(this).parents('li').attr("id");
    var thisBox = $("#"+parentli).children().find(".anthr_usr_cmnt");
    if(thisBox.hasClass("active")){
        thisBox.removeClass("active");
    }else{
        $(".anthr_usr_cmnt.active").removeClass("active");
        thisBox.addClass("active");
    }
});

CSS 的CSS

.active{
   display:block;  
 }

Use $(this) to refer to the event's target object. 使用$(this)引用事件的目标对象。

From there, you can do $(this).find('.some-child-object') or $(this).parents('.some-ancestor') or even $(this).sibblings('div') etc. 从那里,您可以执行$(this).find('.some-child-object')$(this).parents('.some-ancestor')甚至$(this).sibblings('div')等等

$(".slidermenudown .fa-angle-down").on("click",function() {
    var $this = $(this); // "this" is the click's target. 
    var $anthr = $this.find(".anthr_usr_cmnt");
    var $owner = $this.find(".owner_cmnt");

    if ($anthr.hasClass("active")) {
        $anthr.removeClass("active");
    }
    else {
        if ($owner.hasClass("active")) {
            $owner.removeClass("active");
        }
        $anthr.addClass("active");
    }
});

Try this , I have modified your fiddle itself and your css also. 试试这个,我已经修改了你的小提琴本身和你的css。

https://jsfiddle.net/Lse3s3uv/3/ https://jsfiddle.net/Lse3s3uv/3/

 .active{ display:block; } 

  $(".slidermenudown .fa-angle-down").on("click", function() { var $this = $(this).parents('.comment'); // "this" is the click's target. console.log($this); var $anthr = $this.find(".anthr_usr_cmnt"); console.log($anthr); var $owner = $this.find(".owner_cmnt"); console.log($owner); if ($anthr.hasClass("active")) { $anthr.removeClass("active"); } else { if ($owner.hasClass("active")) { $owner.removeClass("active"); } //to remove hide others $('.anthr_usr_cmnt').removeClass('active'); $anthr.addClass("active"); } }); 

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

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