简体   繁体   English

如何使用jQuery隐藏和显示div的一部分

[英]How to hide and show a portion of a div using jQuery

I am working on an application where i need to show a portion of a div while i will collapse it,ie i have may information on that div but i can not hide all information while collapsing.Like i am posting a fiddle here 我正在开发一个应用程序,其中需要显示div的一部分,而我将其折叠,即我在该div上可能有信息,但我无法在折叠时隐藏所有信息。就像我在此处张贴小提琴一样

FIIDDLE FIIDDLE

So in this example when i will collapse after expanding, these portion will be showing. 因此,在此示例中,当我在展开后折叠时,这些部分将显示出来。

Name:Jeet Chatterjee Organization :MSH GROUP Location :Salt Lake Occupation :Software Engineer

This portion only. 仅此部分。

Try this Updated fiddle : 试试这个更新的小提琴

HTML: HTML:

   <a href="#" class="clickme">Click Me</a>
     <div class="box">

   <p> Name:Jeet Chatterjee 
       Organization :MSH GROUP 
       Location :Salt Lake 
       Occupation :Software Engineer
    </p> 

    <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
</div>

Script: 脚本:

    // Hide all the elements in the DOM that have a class of "box"
    $('.box').find('p:last').hide();

    // Make sure all the elements with a class of "clickme" are visible and bound
    // with a click event to toggle the "box" state
    $('.clickme').each(function() {
        $(this).show(0).on('click', function(e) {
            // This is only needed if your using an anchor to target the "box" elements
            e.preventDefault();

            // Find the next "box" element in the DOM
            $(this).next('.box').find('p:last').slideToggle('fast');
        });
    });

You can also use class names to show/hide all of the extra information with a single click, or use an ID to show/hide a specific block of information tied to a specific anchor/click. 您还可以使用类名单击来显示/隐藏所有其他信息,或使用ID来显示/隐藏与特定锚定/单击相关的特定信息块。

For instance, if you wanted the user to click on one link that will expand all of the boxes on the page, then use a class name on the portions that are hidden. 例如,如果您希望用户单击一个链接来展开页面上的所有框,则在隐藏的部分上使用类名。 Or if you wanted each hidden portion to have a specific link that needed to be clicked, you can put a unique ID on each hidden portion that is tied to a clicking on a specific link. 或者,如果您希望每个隐藏部分都有一个需要单击的特定链接,则可以在与单击特定链接相关的每个隐藏部分上放置一个唯一ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM