2

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>

我需要创造一种手风琴。这个想法是只显示标题和带有类 image-with-caption 的 div 内的图像。我在 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,因此代码会影响所有这些,这就是我想要的。

:(

4

1 回答 1

0

您可以将其添加$(this).parent('.portlet').find('.master').show();到 click 事件中h3

于 2011-05-26T15:28:13.217 回答