HTML DOM의 isEqualNode() 메서드는 지정된 두 노드가 서로 동일한지 여부를 판단하여 불리언 값(true/false)을 반환합니다. 이 메서드는 두 요소의 구조와 내용이 완전히 일치하는지 비교해야 할 때 특히 유용하게 활용됩니다.
구문(Syntax)
isEqualNode() 메서드의 기본 구문은 다음과 같습니다.
firstNode.isEqualNode(secondNode)
참고 − 'firstNode'와 'secondNode'는 노드 타입, 속성, 속성값이 모두 같을 경우에만 동일한 노드로 판단됩니다. 자식 노드(childNodes)가 존재한다면 그 하위 내용까지도 완전히 일치해야 합니다.
예제
이제 isEqualNode() 메서드의 실제 사용 예시를 살펴보겠습니다. 아래 예제는 두 개의 게시글 본문을 비교하여 표절 여부를 확인하는 간단한 프로그램입니다.
<!DOCTYPE html>
<html>
<head>
<title>isEqualNode()</title>
<style>
body{
width: 90%;
margin: 0 auto;
}
button{
border-radius:10px;
display:block;
margin:0 auto;
}
#authorJohn, #authorMaya{
border:1px solid black;
border-radius:10px;
}
#showContent{
text-align:center;
}
</style>
</head>
<body>
<div id="authorJohn">
<h2>
Lorem ipsum dolor
</h2>
<h5>By - John</h5>
<p class="content">
sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</div>
</p>
<div>
<div id="authorMaya">
<h2>
Excepteur sint occaecat
</h2>
<h5>By - Maya</h5>
<p class="content">
sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</div>
</p>
<button onclick="checkPlagiarism()">Check Plagiarism</button>
<div id="showContent"></div>
<script>
function checkPlagiarism(){
var articleOne = document.getElementsByClassName("content")[0];
var articleTwo = document.getElementsByClassName("content")[1];
var divDisplay = document.getElementById("showContent");
if(articleOne.isEqualNode(articleTwo))
divDisplay.textContent = 'Content is copied!'
else
divDisplay.textContent = 'Content is not copied!'
}
</script>
</body>
</html>
위 코드에서는 getElementsByClassName()을 사용해 두 개의 <p class="content"> 요소를 가져온 뒤, isEqualNode() 메서드로 두 요소를 비교합니다. 두 노드가 동일하면 'Content is copied!'(내용이 복사됨), 그렇지 않으면 'Content is not copied!'(내용이 복사되지 않음)라는 문구가 화면에 표시되는 방식입니다.
출력 결과
위 코드를 실행하면 다음과 같은 결과를 확인할 수 있습니다.
'Check Plagiarism'(표절 검사) 버튼을 클릭하기 전 −

'Check Plagiarism'(표절 검사) 버튼을 클릭한 후 −
