Phương thức removeNamedItem() trong HTML DOM loại bỏ nút được chỉ định trong tham số từ tập hợp thuộc tính (attribute node) bằng cách sử dụng tên của nó trong tài liệu HTML.
Cú pháp
node.removeNamedItem(node);
Ví dụ
Ví dụ sử dụng phương thức removeNamedItem():
<!DOCTYPE html>
<html>
<head>
<style>
html { height: 100%; }
body {
text-align: center;
color: #fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height: 100%;
}
.btn {
background: #0197F6;
border: none;
height: 2rem;
border-radius: 2px;
width: 60%;
margin: 2rem auto;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Demo phương thức removeNamedItem() DOM</h1>
<p style="color:#db133a;font-size:1.2rem;">Chào! Tôi là một phần tử p trong HTML với một số văn bản ngẫu nhiên</p>
<button onclick="remove()" class="btn">Loại bỏ thuộc tính bằng removeNamedItem()</button>
<script>
function remove() {
var p = document.querySelector("p");
p.attributes.removeNamedItem("style");
}
</script>
</body>
</html>
Kết quả
Nhấn nút Loại bỏ thuộc tính bằng removeNamedItem() để loại bỏ thuộc tính style khỏi phần tử <p>.