CSS + JS make foldable content Blocks

Source: Internet
Author: User
Code Parsing

HTML code

<Div class = "save">

<H1> <a href = "javascript: void (0)" class = "dsphead"
Onclick = "DSP (this)">
<SPAN class = "dspchar"> + </span> heading </a> <Div class = "dspcont"> section </div>
<! -- The preceding part is the first heading. An onclick In H1 triggers the DSP (LOC) function in JS. The initial + number is in the <span> where the class is dspcont, the hidden content is in the DIV whose class is dspcont -->

<H1> <a href = "javascript: void (0)" class = "dsphead"
Onclick = "DSP (this)">
<SPAN class = "dspchar"> + </span> heading </a> <Div class = "dspcont">

<H2> <a href = "javascript: void (0)" class = "dsphead"
Onclick = "DSP (this)">
<SPAN class = "dsphead"> + </span> heading </a> </H2>
<Div class = "dspcont"> section </div>

<H2> <a href = "javascript: void (0)" class = "dsphead"
Onclick = "DSP (this)">
<SPAN class = "dspchar"> + </span> heading </a> </H2>
<Div class = "dspcont"> section </div>
</Div>
</Div>

CSS code

. Save {
Behavior: URL (# default # savehistory );}
/* Prevent text content in the range from being cleared during backoff. [why is this used here not very clear...] */

A. dsphead {
Text-Decoration: none;
Margin-left: 1.5em ;}
/* Set the connection style */

A. dsphead: hover {
Text-Decoration: underline ;}
/* Connection hover status */

A. dsphead span. dspchar {
Font-family: monospace;
Font-weight: normal ;}
/* Set the head and symbol font */

. Dspcont {
Display: none;
Margin-left: 1.5em ;}
/* Set the initial style of the content part, and display is none */

JS Code

<SCRIPT type = "text/JavaScript"> <! --
Function DSP (LOC ){
If (document. getelementbyid ){
FOC = LOC. firstchild. innerhtml?
Loc. firstchild:
Loc. firstchild. nextsibling;/* Set foc to the innerhtml of the next child element of the current vertex, that is, the innerhtml with the + symbol <span> */
FOC. innerhtml = (FOC. innerhtml = '+ ')? '-': '+';/* Determines whether the content in <span> is "-" or "+". If it is "+", it changes to "-" or */
FOC = LOC. parentnode. nextsibling. style?
Loc. parentnode. nextsibling:
Loc. parentnode. nextsibling. nextsibling;/* move the judgment position to the <A> parent (hidden Div behind H1 )*/
FOC. style. Display = FOC. style. Display = 'block '? 'None': 'block';}/* determines whether the display of the DIV is none or block, and sets it to the opposite value */}

If (! Document. getelementbyid)
Document. Write ('<style type = "text/CSS"> <! -- \ N' +
'. Dspcont {display: block;} \ n' +
'// --> </Style> ');
// --> </SCRIPT>/* display all the hidden content in browsers that do not support DOM */

<NoScript>
<Style type = "text/CSS"> <! --
. Dspcont {display: block ;}
// --> </Style>/* display hidden content in browsers that do not support js */
</NoScript>

Related Article

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.