jQuery - 显示/隐藏另一个元素内的元素,然后单击该元素的外部

时间:2011-05-26 14:36:32

标签: jquery content-management-system accordion slidedown slideup

HTML:

<div class="portlet">
<h3 class="">Blogs</h3>
<div class="portlet-content">
    <div class="teaser">
        <div class="image-with-caption">
            <img src="blogs1_peq.jpg" alt="">
        </div>
        <p> Some text here </p>
        <ul class="link-list">
            <li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://theenergycollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">The Energy Collective</a></li>
            <li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://sustainablecitiescollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">Sustainable Cities Collective</a></li>
        </ul>
    </div>
</div>

我需要创造一种手风琴。我们的想法是只显示div中的headeline和图像,带有class-with-caption。我在jQuery中创建了这个代码,将所有内容包装在图像的div下面并将其隐藏起来:

 var $createDiv = $('.image-with-caption').nextAll();
 for(var i=0, len = $createDiv.length; i < len; i+=2){
     $createDiv.slice(i, i+2).wrapAll('<div class="master" />');
 }

我得到这样的东西:

...
<div class="image-with-caption">
    <img src="blogs1_peq.jpg" alt="">
</div>
<div class="master">
    <p> Some text here </p>
    <ul class="link-list">
        <li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://theenergycollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">The Energy Collective</a></li>
        <li xmlns:fofx="urn:FunctionObjectForXsl" class="newwindow"><a href="http://sustainablecitiescollective.com/" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">Sustainable Cities Collective</a></li>
   </ul>
</div>
...

当我点击标题时,它应该显示由jQuery创建和隐藏的新div,但我无法弄清楚我该怎么做。

jQuery代码:

var $createDiv = $('.image-with-caption').nextAll();
for(var i=0, len = $createDiv.length; i < len; i+=2){
    $createDiv.slice(i, i+2).wrapAll('<div class="master" />');
}
$('.master').hide(); //Hide/close all containers


//On Click
$('.portlet h3').click(function(){
    if( $(this).nextAll().is(':hidden') ) { 
        $(this).removeClass('active').next().siblings().next().slideUp();
        $(this).toggleClass('active').next().siblings().next().slideDown(); 
    } else {
        $(this).removeClass('active').siblings().next().siblings().slideUp();
    }
    return false;
});

问题是,我真诚地不知道如何告诉jQuery使用我刚刚点击的h3类主人显示div。不幸的是我无法更改HTML代码,因为这段代码是由CMS生成的,我在同一页面中有几个div.portlet,因此代码会影响所有这些代码,这就是我想要的。

:(

1 个答案:

答案 0 :(得分:0)

您可以将此$(this).parent('.portlet').find('.master').show();添加到h3

的点击事件中