HTML DOM nodeType 속성은 노드 유형에 해당하는 숫자를 반환합니다.
다음은 구문입니다 -
숫자 값 반환
Node.nodeType
여기서 반환 값은 다음과 같을 수 있습니다. -
- 요소 노드에 대한 숫자 '1'
- 속성 노드의 숫자 '2'
- 텍스트 노드의 경우 숫자 '3'
- 댓글 노드의 경우 숫자 '8'
참고:공백은 텍스트 노드로만 간주됩니다.
HTML DOM nodeType의 예를 살펴보겠습니다. 속성 -
예시
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM nodeType</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML-DOM-nodeType</legend>
<label for="example">Example site: </label>
<input id="urlSelect" type="url" name="urlAddress" value="https://www.example.com" disabled>
<input type="button" onclick="getNodeTypes()" value="Get all Types">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var urlSelect = document.getElementById("urlSelect");
var commentNode = document.createComment("My Example URL");
urlSelect.appendChild(commentNode);
var divDisplay = document.getElementById("divDisplay");
function getNodeTypes() {
divDisplay.textContent = 'Body: '+document.body.nodeType;
divDisplay.textContent += ' & input: '+urlSelect.nodeType;
divDisplay.textContent += ' & comment: '+commentNode.nodeType;
}
</script>
</body>
</html> 출력
'모든 유형 가져오기를 클릭하기 전에 ' 버튼 -

'모든 유형 가져오기를 클릭한 후 ' 버튼 -
