Computer >> Máy Tính >  >> Lập trình >> HTML

Thuộc tính loại HTML DOM Fieldset

Thuộc tính kiểu tập trường DOM HTML được sử dụng để trả về loại phần tử tập trường. Nó sẽ luôn thuộc loại tập trường cho một phần tử tập trường. Đây là thuộc tính chỉ đọc.

Cú pháp

Sau đây là cú pháp cho thuộc tính kiểu Fieldset -

fieldsetObject.type

Ví dụ

Chúng ta hãy xem một ví dụ về thuộc tính kiểu Fieldset -

<!DOCTYPE html>
<html>
<head>
<script>
   function FieldType() {
      var field = document.getElementById("FieldSet1").type;
      document.getElementById("Sample").innerHTML = "The fieldset element is of type "+field;
   }
</script>
</head>
<body>
<h1>Sample FORM</h1>
<form id="FORM1">
<fieldset id="FieldSet1">
<legend>User Data:</legend>
Name: <input type="text"><br>
Address: <input type="text"><br>
Age: <input type="text">
</fieldset>
</form>
<br>
<button onclick="FieldType()">GET TYPE</button>
<p id="Sample"></p>
</body>
</html>

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Thuộc tính loại HTML DOM Fieldset

Khi nhấp vào nút NHẬN LOẠI -

Thuộc tính loại HTML DOM Fieldset

Trong ví dụ trên -

Chúng tôi đã tạo phần tử tập trường với id “FieldSet1” bên trong phần tử biểu mẫu−

<form id="FORM1"> <fieldset id="FieldSet1"> <legend>User Data:</legend> Name: <input type="text"><br> Address: <input type="text"><br> Age: <input type="text"> </fieldset> </form>

Sau đó, chúng tôi đã tạo một nút “GET TYPE” sẽ thực thi hàm FieldType () khi người dùng nhấp vào -

<button onclick="FieldType()">GET TYPE</button>

Phương thức FieldType () sẽ lấy giá trị thuộc tính kiểu fieldet và gán nó cho trường biến. Vì kiểu cho phần tử tập trường sẽ luôn là tập trường, nó sẽ trả về tập trường giá trị. Giá trị này sau đó được hiển thị trong một đoạn văn có id là "Sample" bằng cách sử dụng thuộc tính innerHTML -

function FieldType() {
   var field = document.getElementById("FieldSet1").type;
   document.getElementById("Sample").innerHTML = "The fieldset element is of type "+field;
}