JavaScript에서 지정된 속성 노드를 제거하고 제거된 노드를 반환하는 방법은 무엇인가요?


JavaScript에서는 removeAttributeNode() 메소드를 사용하여 지정된 속성 노드를 제거하고 제거된 노드를 반환할 수 있습니다. 이 메소드는 Element.removeAttributeNode()를 호출하여 요소에서 지정된 속성 노드를 제거합니다. 제거된 노드는 반환되며, 이를 변수에 할당하여 나중에 사용할 수 있습니다.

다음은 removeAttributeNode() 메소드를 사용하여 data-attribute 속성 노드를 제거하고 제거된 노드를 반환하는 예시입니다.

const element = document.getElementById("myElement");
const attributeNode = element.removeAttributeNode(element.getAttributeNode("data-attribute"));
console.log(attributeNode); // 제거된 노드 출력

또 다른 방법으로는 Element.attributes 속성을 사용하여 요소의 모든 속성 노드를 가져온 다음, NamedNodeMap 객체에서 removeNamedItem() 메소드를 사용하여 지정된 속성 노드를 제거할 수 있습니다. 이 방법은 다음과 같이 구현할 수 있습니다.

const element = document.getElementById("myElement");
const attributes = element.attributes;
const attributeNode = attributes.removeNamedItem("data-attribute");
console.log(attributeNode); // 제거된 노드 출력


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.