Simple click show/close effect (native JS version and JQ Version)

Source: Internet
Author: User

Extensible simple Click Show/close effects are implemented using native JavaScript and jquery. For usage and Related explanations, see source code.
In addition, it is better to teach people to fish. I wrote only a prototype, providing a method of thinking. I also noted in the remarks. If you want the cool effect, you can expand according to your actual needs.
View Demo:Click here to view the demo
Native JS version:

  1. Window.Onload=Function(){
  2. var divs = document . getelementsbytagname ( ' Div ' ) ; // locate all DIV elements
  3. for ( var I = 0 ; I divs . length ; I ++ ) {
  4. If(Divs[I].Classname! ='Jsdemo') Continue;// If the element class value is not jsdemo, continue searching
  5. // Divs [I]. style. Display = 'none'; // if you want to hide it by default, you can cancel this line of comment.
  6. var title = divs [ I ] . previussibling ; // locate the title element from the previous element
  7. If(Title.Nodetype! =1){// To ensure that the element node is found
  8. Title=Title.Previussibling;
  9. }
  10. Title.Next=Divs[I];// Set the next attribute of the title and point to Div [I]
  11. Title.Onclick=Function(){// Click an event
  12. VaR Curstyle=This.Next.Style.Display;// Take the default display value of Div [I]. Now you know the intention of title. Next.
  13. VaR Newstyle;// Define a new display value
  14. var ICO = title . getelementsbytagname ( ' span ' ) [ 0 ] ; // nodes that contain the show and close button
  15. If(Curstyle='None'){// Click expand to close the retrieval result.
  16. Newstyle='Block';// Switch to visible when the default value is hidden
  17. ICO.Innerhtml='-';// Switch the Show button
  18. }Else{
  19. Newstyle='None';// Switch to hidden when the default value is visible
  20. ICO.Innerhtml='+';// Switch the Show button
  21. };
  22. Title.Next.Style.Display=Newstyle;// Assign a value to the DIV [I] After clicking
  23. }
  24. }
  25. }

JQ version:

  1. $(Function(){
  2. VaR$Title= $('Div. jqdemo');// Find the element to display/hide
  3. /// $ Title. Hide (); // if you want to hide it by default, you can cancel this line comment. The following two switching functions change the position
  4. $ title . Prev () . toggle ( function () { // display or hide the previous element (retrieve the title element) set click switching event
  5. $ title . hide () . Prev () . Find ( ' span ' ) . text ( ' + ' ) ; // when you click, hide the DIV element, locate the span in the title element, and click
  6. } , function () {
  7. $ title . show () . Prev () . Find ( ' span ' ) . text ( ' - ' ) ; // when you click the second time, show the DIV element, locate the span in the title element, and click the switch button
  8. });
  9. // JQCodeIsn't it concise and beautiful? haha. Just a few lines of code to get it done. By modifying show ()/hide (), you can get cool results.
  10. });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.