Đối tượng HTML đại diện cho phần tử của tài liệu HTML.
Hãy để chúng tôi xem cách truy cập đối tượng HTML -
Cú pháp
Sau đây là cú pháp -
document.getElementsByTagName(“HTML”)
Hãy để chúng tôi xem một ví dụ về đối tượng HTML -
Ví dụ
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align:center;
}
.btn{
background-color:lightblue;
border:none;
height:2rem;
border-radius:50px;
width:60%;
margin:1rem auto;
}
.show{
font-size:1rem;
font-weight:bold;
color:orange;
border:2px solid green;
padding:10px;
display:none;
}
</style>
</head>
<body>
<h1>DOM HTML Object Example</h1>
<button type="button" onclick="getContent()" class="btn">Click me to get content
inside HTML tag</button>
<div class="show"></div>
<script>
function getContent(){
var content=document.getElementsByTagName("HTML")[0].innerHTML;
document.querySelector(".show").innerHTML = content;
document.querySelector(".show").style.display ="block";
}
</script>
</body>
</html> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Nhấp vào nút “xanh lam” để nhận tất cả nội dung bên trong thẻ trang này -