Đối tượng Pre trong HTML DOM đại diện cho phần tử <pre> của một tài liệu HTML. Phần tử này dùng để hiển thị văn bản được định dạng trước (preformatted text).
Tạo đối tượng pre
Cú pháp
document.createElement("PRE");
Ví dụ
Ví dụ tạo đối tượng <pre> động:
<!DOCTYPE html>
<html>
<head>
<style>
body { text-align: center; background-color: #fff; color: #0197F6; }
h1 { color: #23CE6B; }
.btn {
background-color: #fff;
border: 1.5px dashed #0197F6;
height: 2rem;
border-radius: 2px;
width: 60%;
margin: 2rem auto;
display: block;
color: #0197F6;
outline: none;
cursor: pointer;
}
pre { font-size: 1.2rem; color: #db133a; }
</style>
</head>
<body>
<h1>Demo đối tượng Pre DOM</h1>
<button onclick="createPre()" class="btn">Tạo đối tượng pre</button>
<script>
function createPre() {
var preElement = document.createElement("PRE");
preElement.innerHTML = "Chào! Tôi là một phần tử pre HTML";
document.body.appendChild(preElement);
}
</script>
</body>
</html>
Kết quả
Nhấn nút Tạo đối tượng pre để tạo một đối tượng pre.