Thuộc tính HTML DOM tagname trả về tên thẻ của phần tử HTML trong tài liệu HTML.
Cú pháp
Sau đây là cú pháp -
element.tagName
Hãy để chúng tôi xem một ví dụ về thuộc tính HTML DOM tagName−
Ví dụ
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background-color: #FBAB7E;
background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);
text-align: center;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem auto;
}
</style>
</head>
<body>
<h1>HTML DOM tagName Property Demo</h1>
<button class="btn" onclick="display()">Click me to show my tag name</button>
<div class="show"></div>
<script>
function display() {
document.querySelector('.show').innerHTML = 'Tag name is: ' + document.querySelector('button').tagName;
}
</script>
</body>
</html> Đầu ra
Nhấp vào “ Nhấp vào tôi để hiển thị tên thẻ của tôi ”Để hiển thị tên thẻ của màu đỏ nút.