JavaScript traversal of HTML form elements and form definitions, javascript form

Source: Internet
Author: User

JavaScript traversal of HTML form elements and form definitions, javascript form

The following JavaScript code traverses all HTML elements (html dom elements) through the document object ).

<Html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "> 

Note: Code explanation
(1). hasAttribute ("id") to determine whether the "id" attribute exists. If a specified attribute exists, the hasAttribute () method returns true; otherwise, false;
(2). id, return the element id;
(3). getAttribute ("fieldtype") returns the specified attribute value of the element node.

Using the above Code, we will consider the following:

  • When defining a form, key information of the form data item, such as the data type, is directly defined through the attribute of the element;
  • By traversing the form, get the form design definition and its business data value, and directly convert the two to BSON type storage high MongoDB;
  • When presenting or editing a form, define the form design as a template and write it back to set the data content.

The following content is provided for reference to W3School.

Html dom Document Object

Document Object
Each HTML Document loaded into the browser will become a Document Object.
The Document object allows us to access all elements in the HTML page from the script.
Tip: The Document object is part of the Window object and can be accessed through the Document DOC ument attribute.

Html dom getElementsByTagName () method
  • Definition and usage
The getElementsByTagName () method returns a set of objects with the specified tag name.
  • Syntax
document.getElementsByTagName(tagname)
  • Description

The getElementsByTagName () method returns the order of elements in the document.
If you pass the special string "*" to the getElementsByTagName () method, it returns a list of all elements in the document. The order of elements is the order in the document.

Html dom Element Object html dom Node

In html dom (Document Object Model), each part is a node:

  • The document itself is a document Node
  • All HTML elements are element nodes.
  • All HTML attributes are attribute nodes.
  • The text in the HTML element is a text node.
  • Annotation is a comment node.

Element Object

  • In the html dom, an Element object represents an HTML Element.
  • Element objects can have child nodes of the Element node, text node, and comment node types.
  • The NodeList object indicates the node list, such as the child node set of the HTML element.
  • The element can also have attributes. Attribute is an attribute node
Html dom nodeType attributes

Return values of nodeName and nodeValue attributes for each node type:

Serial number Node Type NodeName returned Return nodeValue
1 Element Element name Null
2 Attr Attribute name Attribute Value
3 Text Text Node content
4 CDATASection Cdata-section Node content
5 EntityReference Object Reference name Null
6 Entity Entity name Null
7 ProcessingInstruction Target Node content
8 Comment Comment Comment text
9 Document Document Null
10 DocumentType Document Type name Null
11 DocumentFragment Document Fragment Null
12 Notation Symbol name Null

Refer:
W3School offers: http://www.w3school.com.cn/jsref/dom_obj_document.asp

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.