Javascript Dom programming (1)

Source: Internet
Author: User

Do you know about Javascript Dom programming? In the DOM model, every element, attribute, and text can be considered as an object, and javascript can access these objects independently, you can use some methods to find and change these objects.

Javascript Dom Programming

I. Document Object Model

In the DOM model, each element (element), attribute), and text can be viewed as an object. javascript can access these objects independently, you can use some methods to find and change these objects.

DOM stipulates that each HTML Tag is an element node, and the text contained in the element is a text node, and each HTML attribute is an attribute node.

Ii. Accessing DOM nodes

A: Get elements by ID

 
 
  1. vartarget=document.getElementById("berenger");  
  2.  

B: Get the element through TagName

 
 
  1. varlistItems=document.getElementsByTagName("li");  
  2.  

ListItems is an array-like object. You can use listItems. Length to obtain the object Length.

C: Get the element through ClassName

In most cases, it is easier to use className to obtain elements than tagname in Javascript Dom programming. However, dom does not provide corresponding functions, so we need to create a method.

It can be divided into the following three steps:

1: search for all elements in the document.

2: For each element, compare the class to be searched.

3: if they are the same, add them to the list.

The js Code is as follows:

 
 
  1. VarCore = {};
  2.  
  3. Core. getElementsByClass = function (theClass)
  4. {
  5. VarelementArray = [];
  6. // GetElementsByTagName ("*") is not supported in IE, and document. All is used.
  7. If (document. all)
  8. {
  9. ElementArray = document. all;
  10. }
  11. Else
  12. {
  13. ElementArray = document. getElementsByTagName ("*");
  14. }
  15. VarmatchedArray = [];
  16. Varpattern = newRegExp ("(^ |)" + theClass + "(| $ )");
  17. For (vari = 0; I <elementArray. length; I ++)
  18. {
  19. If (pattern. test (elementArray [I]. className ))
  20. {
  21. MatchedArray [matchedArray. length] = elementArray [I];
  22. }
  23. }
  24. ReturnmatchedArray;
  25. };
  26.  


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.