0

I' am using jQuery to get the previous DIV with Class "our-partner-readmore". I have tried alot but was not possible in doing it.

HTML

<div class="our-partner-readmore"><a class="accordion-toggle btn-readmore collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapse2"></a></div>    

<div class="our-partner-readmore"><a class="accordion-toggle btn-readmore collapsed" data-toggle="collapse" data-parent="#accordion2" href="#collapse2"></a></div>

JS

$('a.btn-readmore').on("click", function(){
    var is_collapsed = $(this).hasClass("collapsed");
    alert(is_collapsed);
        if(is_collapsed){
            $(this).prev('.our-partner-readmore').addClass("none");
            $(this).next('.our-partner-readmore-closed').removeClass("none");
        } else {
            $(this).prev('.our-partner-readmore-closed').addClass("none");
            $(this).next('.our-partner-readmore').removeClass("none");
    }
});

More Explaination

Whenever some clicks on the "a.btn-readmore", Using jQuery am trying to get the previous div class "our-partner-readmore" dynamically.

Thanks guys

4

3 回答 3

1

您的问题可能会使用 jquery 父函数解决。这是 api详细信息

$(this).parent(); //Will give you the parent
于 2013-08-08T03:56:29.497 回答
1
         $(this).prev and   $(this).next are to search for the siblings 

由于您要在其上应用课程的 div 是单击链接的父级,因此首先要转到其父级

所以你可以像这样编写代码

        $('a.btn-readmore').on("click", function(){
            var is_collapsed = $(this).hasClass("collapsed");
            alert(is_collapsed);
                if(is_collapsed){
                    $(this).parents('.our-partner-readmore').prev().addClass("none");
                    $(this).parents('.our-partner-readmore-closed').next().removeClass("none");
                } else {
                    $(this).parents('.our-partner-readmore-closed').addClass("none");
                    $(this).parents('.our-partner-readmore').removeClass("none");
            }
        });
于 2013-08-08T04:14:04.307 回答
0

.prev()给你元素的前一个兄弟,你需要的是一个祖先元素

您也可以使用.closest()查找与给定选择器匹配的最近祖先

$(this).closest('.our-partner-readmore').addClass("none");
于 2013-08-08T04:02:36.310 回答