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:
-
- Window.Onload=Function(){
- var divs = document . getelementsbytagname ( ' Div ' ) ; // locate all DIV elements
- for ( var I = 0 ; I divs . length ; I ++ ) {
- If(Divs[I].Classname! ='Jsdemo') Continue;// If the element class value is not jsdemo, continue searching
-
- // Divs [I]. style. Display = 'none'; // if you want to hide it by default, you can cancel this line of comment.
- var title = divs [ I ] . previussibling ; // locate the title element from the previous element
- If(Title.Nodetype! =1){// To ensure that the element node is found
-
- Title=Title.Previussibling;
-
- }
- Title.Next=Divs[I];// Set the next attribute of the title and point to Div [I]
-
- Title.Onclick=Function(){// Click an event
- VaR Curstyle=This.Next.Style.Display;// Take the default display value of Div [I]. Now you know the intention of title. Next.
-
- VaR Newstyle;// Define a new display value
- var ICO = title . getelementsbytagname ( ' span ' ) [ 0 ] ; // nodes that contain the show and close button
- If(Curstyle='None'){// Click expand to close the retrieval result.
-
- Newstyle='Block';// Switch to visible when the default value is hidden
- ICO.Innerhtml='-';// Switch the Show button
-
- }Else{
- Newstyle='None';// Switch to hidden when the default value is visible
-
- ICO.Innerhtml='+';// Switch the Show button
- };
-
- Title.Next.Style.Display=Newstyle;// Assign a value to the DIV [I] After clicking
-
- }
-
- }
-
- }
JQ version:
-
- $(Function(){
-
- VaR$Title= $('Div. jqdemo');// Find the element to display/hide
-
- /// $ Title. Hide (); // if you want to hide it by default, you can cancel this line comment. The following two switching functions change the position
- $ title . Prev () . toggle ( function () { // display or hide the previous element (retrieve the title element) set click switching event
- $ title . hide () . Prev () . Find ( ' span ' ) . text ( ' + ' ) ; // when you click, hide the DIV element, locate the span in the title element, and click
- } , function () {
- $ 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
- });
-
- // 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.
-
- });