【问题标题】:Toggle whichever element is clicked切换单击的任何元素
【发布时间】:2008-12-20 21:04:57
【问题描述】:

我在 jQuery 中使用 toggle(),我的页面上有一个侧边栏,每个标题都是

<h2 class="sidebar-header">

我的代码的一部分如下所示:

    <div class="sidebar-section">   
        <h2 class="sidebar-header">SUBSCRIBE</h2>
        <p class="sidebar">Make sure you subscribe to stay in touch with the latest articles and tutorials. Click on one of the images below:</p>
        <div class="rss-icons">
            <a href="http://fusion.google.com/add?feedurl=http://feeds.feedburner.com/ryancoughlin"><img src="http://gmodules.com/ig/images/plus_google.gif" width="62" height="17" alt="Add to Google Reader or Homepage" class="feed-image"/></a> 
            <a href="http://add.my.yahoo.com/rss?url=http://feeds.feedburner.com/ryancoughlin" title="Web/Graphic Design/Development by Ryan Coughlin"><img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo3.gif" alt="Add feed to My Yahoo" width="62" height="17" class="feed-image" /></a>
            <a href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http://feeds.feedburner.com/ryancoughlin" title="Web/Graphic Design/Development by Ryan Coughlin"><img src="http://www.newsgator.com/images/ngsub1.gif" alt="Subscribe in NewsGator Online" class="feed-image" height="17" /></a>
        </div>
        <hr />
        <p class="sidebar feed">Don't have those? Grab the <a href="http://feeds.feedburner.com/ryancoughlin" target="_blank" title="Subscribe to this feed">RSS</a> url.</p>
        <p class="sidebar delicious">Make sure you add me to <a href="http://del.icio.us/post?url=http://www.ryancoughlin.com&amp;title=Ryan Coughlin - Web/Graphic Design and Development" target="_blank" title="Add to delicious!">delicious!</a> </p>
    </div>

它们都被包裹在那些 DIV 元素中。如果您单击标题,我正在尝试这样做,它会缩小内容。我知道切换可以做到这一点,但如果我为每个“侧边栏标题”制作它,您将单击页面上的任何一个元素,它会将它们全部隐藏,我该怎么做?

【问题讨论】:

    标签: javascript jquery toggle


    【解决方案1】:

    试试这样的:

    假设你有以下代码:

    <div class="topdiv">
        <h2 class="header">Header 1</h2>
        <div class="somecontent">
            This is the content
        </div>
    </div>
    
    <div class="topdiv">
        <h2 class="header">Header 2</h2>
        <div class="somecontent">
            This is the content
        </div>
    </div>
    

    现在,说当你想点击一个标题时,那个标题的内容会显示/隐藏:

    $(".header").click(function () {
        $(this).parent(".topdiv:first").find(".somecontent").toggle();
    });
    

    这样,只有特定标题的内容会被切换,而不是其余的。

    现在您可以分析我为您编写的代码,并将其应用到您自己的上下文中。

    【讨论】:

    • 所以当您单击 .header 时,这将起作用,它将获取该元素和 div.topdiv 的父元素并找到 .somecontent 然后应用切换?正确的?谢谢你的帮助!瑞安
    • 是的,它会找到被点击的标题,遍历它的父 div 有一个 'topdiv' 类,然后遍历那个 'topdiv' 的子元素,它有一个 'somecontent' 类,并切换它。
    【解决方案2】:

    试试这个: nextAll

        $(".sidebar-header").click(function() {
            $(this).nextAll().toggle();
        });
    

    【讨论】:

    • 这似乎成功了! nextAll() 的工作原理很有趣,我仍然对它的工作原理感到困惑,所有兄弟姐妹?我知道这有点“广泛”,但是,这到底是什么“意思”谢谢你,Max
    • 当您调用“nextAll”时,jQuery 在内部使用“nextSibling”DOM 元素属性。并且有一个简单的“while”循环来扫描当前元素之后的所有兄弟姐妹。您甚至可以在 jQuery 源代码中自己探索它:找到“nextAll”,然后查看相应的“dir”调用 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2015-11-11
    • 2020-02-29
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多