HTML DOM Properties and Methods
2011-07-17 11:03
429 查看
Properties and methods define the programming interface of the HTML DOM.
x.nodeName - the name of x
x.nodeValue - the value of x
x.parentNode - the parent node of x
x.childNodes - the child nodes of x
x.attributes - the attributes nodes of x
Note: In the list above, x is a node object (HTML element).
x.getElementsByTagName(name) - get all elements with a specified tag name
x.appendChild(node) - insert a child node to x
x.removeChild(node) - remove a child node from x
Note: In the list above, x is a node object (HTML element).
In the example above, getElementById is a method, while innerHTML is a property.
In the example above, getElementById is a method, while childNodes and nodeValue are properties.In this tutorial we will mostly use the innerHTML property. However, learning the method above is useful for understanding the tree structure of the DOM and dealing with XML files.
Programming Interface
In the DOM, HTML documents consist of a set of node objects. The nodes can be accessed with JavaScript or other programming languages. In this tutorial we will use JavaScript.The programming interface of the DOM is defined by standard properties and methods.Properties are often referred to as something that is (i.e. the name of a node).Methods are often referred to as something that is done (i.e. remove a node).HTML DOM Properties
Some DOM properties:x.innerHTML - the text value of xx.nodeName - the name of x
x.nodeValue - the value of x
x.parentNode - the parent node of x
x.childNodes - the child nodes of x
x.attributes - the attributes nodes of x
Note: In the list above, x is a node object (HTML element).
HTML DOM Methods
Some DOM methods:x.getElementById(id) - get the element with a specified idx.getElementsByTagName(name) - get all elements with a specified tag name
x.appendChild(node) - insert a child node to x
x.removeChild(node) - remove a child node from x
Note: In the list above, x is a node object (HTML element).
The innerHTML Property
The easiest way to get or modify the content of an element is by using the innerHTML property.innerHTML is not a part of the W3C DOM specification. However, it is supported by all major browsers.The innerHTML property is useful for returning or replacing the content of HTML elements (including <html> and <body>), it can also be used to view the source of a page that has been dynamically modified.Example
The following code gets the innerHTML (text) from the <p> element with id="intro":Example
|
childNodes and nodeValue
We can also use the childNodes and nodeValue properties to get the content of an element.The following code gets the value of the <p> element with id="intro":Example
|
相关文章推荐
- Microsoft.XMLDOM(XMLDOM)对象的属性和方法(Methods and properties of Microsoft.XMLDOM)
- Layout Properties And Methods In Element, HTMLElement And Window
- Microsoft.XMLDOM(XMLDOM)对象的属性和方法(Methods and properties of Microsoft.XMLDOM)
- Html Dom add and delete element
- Objective-C Primer(2)Private Methods and Class Properties
- Methods GET and POST in HTML forms - what's the difference?
- Properties, methods and events
- 1st JavaScript Editor Pro 3.8,强大的JavaScript开发工具(JavaScript IDE),完美支持CSS, HTML, DOM and DHTML开发、校验、调试
- DOM objects and methods
- Dom and innerHTML
- DOM attributes and properties
- The Boolean object's properties and methods
- Professional JS(5.5.5-function properties and methods&&5.6primitive wrapper type)
- JavaScript: What's the difference between HTML attribute and DOM property?
- Date object's properties and methods
- Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
- Math object's properties and methods
- JavaScript Patterns 5.3 Private Properties and Methods
- Xrm.Page.data.entity Properties and Methods
- String object's properties and methods