본문 바로가기

JavaScript

HTML DOM 함수 - document interface

# Document interface

1. getElementById(id)

: Id를 이용해 Element에 접근할 때 사용하는 함수이다. 태그의 id를 매개변수로 받아서 id에 해당하는 element를 반환해준다.

getElementById introduced in DOM Level 2
Returns the Element that has an ID attribute with the given value. If no such element exists, this returns null. If more than one element has an ID attribute with that value, what is returned is undefined.
The DOM implementation is expected to use the attribute Attr.isId to determine if an attribute is of type ID.

Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.

Parameters
elementId of type DOMString
The unique id value for an element.
Return Value

Element

The matching element or null if there is none.

 

2. getElementsByTagName(name)

:  TagName을 이용해 Element에접근할 때 사용한다. 태그의 이름을 매개변수로 받으며 일치하는 태그 이름의 Elements를 반환한다.

여러개의 Element가 반환되면 배열로 값이 반환.

getElementsByTagName
Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.
Parameters
tagname of type DOMString
The name of the tag to match on. The special value "*" matches all tags. For XML, the tagname parameter is case-sensitive, otherwise it depends on the case-sensitivity of the markup language in use.
Return Value

NodeList

A new NodeList object containing all the matched Elements.

 

3. createElement(tagName)

: 새로운 Element 생성한다. 해당 태그이름의 Element를 새롭게 생성한 뒤 Element Object를 반환한다.

createElement
Creates an element of the type specified. Note that the instance returned implements the Element interface, so attributes can be specified directly on the returned object.
In addition, if there are known attributes with default values, Attr nodes representing them are automatically created and attached to the element.
To create an element with a qualified name and namespace URI, use the createElementNS method.
Parameters
tagName of type DOMString
The name of the element type to instantiate. For XML, this is case-sensitive, otherwise it depends on the case-sensitivity of the markup language in use. In that case, the name is mapped to the canonical form of that markup by the DOM implementation.
Return Value

Element

A new Element object with the nodeName attribute set to tagName, and localName, prefix, and namespaceURI set to null.

Exceptions

DOMException

INVALID_CHARACTER_ERR: Raised if the specified name is not an XML name according to the XML version in use specified in the Document.xmlVersion attribute.

4. createTextNode(data)

: 새로운 TextNode를 생성한다. data를 가지는 TextNode를 생성한 뒤 Text Object를 반환한다.

createTextNode
Creates a Text node given the specified string.
Parameters
data of type DOMString
The data for the node.
Return Value

Text

The new Text object.

No Exceptions

 

 

'JavaScript' 카테고리의 다른 글

JSON  (0) 2016.04.08
Node interface  (0) 2016.04.07
DOM  (0) 2016.04.06
내장 객체 - String  (0) 2016.04.05
Array  (0) 2016.04.04