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

发布于 2024-11-10 08:06:45 字数 2972 浏览 6 评论 0原文

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 内带有 image-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 下方显示带有类 master 的 div。不幸的是,我无法更改 HTML 代码,因为该代码是由 CMS 生成的,并且我在同一页面中有多个 div.portlet,因此该代码将影响所有这些,这就是我想要的。

:(

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>

I need to create a sort of accordion. The idea is to display only the headeline and the image inside the div with the class image-with-caption. I made this code in jQuery to wrap everything bellow the div of the image and hide it:

 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" />');
 }

And I get something like this:

...
<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>
...

When I click on the headline it should show me the new div created and hidden by jQuery, but I can't figure out how can I do this.

jQuery Code:

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;
});

The problem is, I sincerely don't know how to tell jQuery to display the div with the class master bellow the h3 I just clicked. Unfortunately I can't change the HTML code, since this code is generated by a CMS, and I have several div.portlet in the same page, so the code will affect all of them, and that's what I want.

:(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-11-17 08:06:45

您可以将此 $(this).parent('.portlet').find('.master').show(); 添加到 h3 的点击事件中

You could add this $(this).parent('.portlet').find('.master').show(); to the click event for the h3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文