"Javascript Dom Reading notes" Chapter8 Enrich document content

Source: Internet
Author: User

Purpose of this chapter

The authors cite the first example of dynamically creating a list of abbreviations (abbreviation) for a Web page. As you know, we can use <abbr>...</abbr> to indicate an abbreviation, such as <abbr title= "Document Object Model" >DOM</ABBR> The title attribute indicates the full name of the abbreviation. All we have to do is find out all the <abbr> and its corresponding title in the article, summarize it, and then display the abbreviation table at the end of the document. The final effect should look like this:

Specific steps

We can do it in three steps:

    • Traverse document for information
    • Create label wrapper information
    • Inserting tags into a WEB document

The specific JS code is as follows:

1 //JavaScript Document2 functiondisplayabbreviations () {3     if(!document.getelementsbytagname | |!document.createelement | |!document.createtextnode)return false;4     //Get all Acronyms5     varAbbreviations=document.getelementsbytagname ("abbr");6     if(abbreviations.length<1)return false;7     vardefs=NewArray ();8     //Traverse these acronyms9      for(vari=0;i<abbreviations.length;i++){Ten         varCurrent_abbr=Abbreviations[i]; One         if(current_abbr.childnodes.length<1)Continue; A         varDefinition=current_abbr.getattribute ("title"); -         varkey=Current_abbr.lastChild.nodeValue; -defs[key]=definition; the     } -     //Create a definition list -     varDlist=document.createelement ("DL"); -      for(Keyinchdefs) { +         varDefiniton =Defs[key]; -         varDtitle=document.createelement ("DT"); +         vardtitle_text=document.createTextNode (key); A Dtitle.appendchild (dtitle_text); at         varDdesc=document.createelement ("DD"); -         varddesc_text=document.createtextnode (definition); - Ddesc.appendchild (ddesc_text); - Dlist.appendchild (dtitle); - Dlist.appendchild (DDESC); -     } in     if(dlist.childnodes.length<1)return false; -     varHeader=document.createelement ("H2"); to     varHeader_text=document.createtextnode ("Abbreviations"); + Header.appendchild (header_text); - Document.body.appendChild (header); the Document.body.appendChild (dlist); * } $ Panax Notoginseng //function is called when the page is loaded -Addloadevent (displayabbreviations);

Here the emphasis is placed on the 18 line for the (variable in array) traversal of all elements of the array!!

Summarize

(1) using the correct label to build the page is the premise of using JS to promote the content of the page

(2) using JS to enhance the content of the page than directly in the page to add tags, more flexible, efficient, automatic, easy to modify.

(3) You can use the technical area described in this chapter to write some scripts, extract the document title at the beginning as can reach the relevant title of the internal link (can refer to the online information to complete)

"Javascript Dom Reading notes" Chapter8 Enrich document content

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.