Đối tượng nhỏ HTML DOM được liên kết với phần tử HTML . Chúng ta có thể tạo và truy cập phần tử nhỏ bằng phương thức createElement () và getElementById () tương ứng.
Cú pháp
Sau đây là cú pháp cho -
Tạo một đối tượng nhỏ -
var s= document.createElement("SMALL"); Ví dụ
Chúng ta hãy xem một ví dụ cho đối tượng nhỏ -
<!DOCTYPE html>
<html>
<body>
<h1>Small object example</h1>
<p>Create a small element containing some text by clicking the below button</p>
<button onclick="CreateSmall()">CREATE</button>
<p>This is normal text</p>
<script>
function CreateSmall() {
var s = document.createElement("SMALL");
var txt = document.createTextNode("This is small text");
s.appendChild(txt);
document.body.appendChild(s);
}
</script>
</body>
</html> Đầu ra
Điều này sẽ tạo ra kết quả sau -
Khi nhấp vào nút TẠO -