Tạo thuộc tính trong JavaScript là gì?

Phương thức createAttribute() tạo một thuộc tính từ tên đã chỉ định và trả về thuộc tính dưới dạng đối tượng Attr

Hỗ trợ trình duyệt

Tạo thuộc tính trong JavaScript là gì?
Tạo thuộc tính trong JavaScript là gì?
Tạo thuộc tính trong JavaScript là gì?
Tạo thuộc tính trong JavaScript là gì?
Tạo thuộc tính trong JavaScript là gì?
createAttributeCóCóCóCóCó

cú pháp

document.createAttribute(attributename)

Thông số

Tham sốTypeDescriptionattributenameAttr objectBắt buộc. Tên của thuộc tính bạn muốn tạo

Giá trị trả về

TypeDescriptionNode objectThuộc tính đã tạo

Thí dụ

Đoạn mã sau cho biết cách tạo một thuộc tính lớp, với giá trị "democlass" và chèn nó vào phần tử H1

Phương thức HTML DOM createAttribute() được sử dụng để tạo một thuộc tính cụ thể bằng JavaScript cho một phần tử HTML. Phương thức createAttribute() tạo một thuộc tính với tên đã cho và trả về thuộc tính dưới dạng đối tượng Attr

cú pháp

Sau đây là cú pháp cho phương thức createAttribute() −

document.createAttribute(attributename)

Thí dụ

Chúng ta hãy xem một ví dụ cho phương thức HTML DOM createAttribute() -



CREATE ATTRIBUTE



Click the button to create a "class" attribute for the div element

CREATE

This is a sample div

đầu ra

Điều này sẽ tạo ra đầu ra sau -

Tạo thuộc tính trong JavaScript là gì?

Khi nhấp vào nút TẠO -

Tạo thuộc tính trong JavaScript là gì?

Trong ví dụ trên -

Chúng tôi đã tạo một phong cách với id “DIV1”

#DIV1{
   margin-top:15px;
   width:250px;
   height:200px;
   border:2px solid blue;
   background-color:lightgreen;
}

Sau đó chúng tôi đã tạo ra một

phần tử có một

phần tử bên trong nó

This is a sample div

Sau đó, chúng ta đã tạo một nút CREATE sẽ thực thi hàm attrCreate() khi người dùng nhấp vào -

Phương thức createAttribute() này được sử dụng để tạo một thuộc tính với tên được chỉ định và trả về đối tượng thuộc tính. thuộc tính. Thuộc tính giá trị được sử dụng để đặt giá trị của thuộc tính và phần tử. Phương thức setAttribute() được sử dụng để tạo một thuộc tính mới cho một phần tử. Phương thức này () chứa tên của thuộc tính đã tạo dưới dạng giá trị tham số.  

cú pháp

document.createAttribute( attributename )

Thí dụ.  

html




________số 8

<

document.createAttribute(attributename)
0
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2<
document.createAttribute(attributename)
4
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<121
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<7
document.createAttribute(attributename)
1

9<0

<1<2

<1<4

<1<6

9<8

9

document.createAttribute(attributename)
00

<1

document.createAttribute(attributename)
02

9<8

document.createAttribute(attributename)
6
document.createAttribute(attributename)
06____87
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2
document.createAttribute(attributename)
06______14
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2____9____115
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<
document.createAttribute(attributename)
19
document.createAttribute(attributename)
20
document.createAttribute(attributename)
19
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<
document.createAttribute(attributename)
25
document.createAttribute(attributename)
26
document.createAttribute(attributename)
25
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<<1 <2<1
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<<7
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6
document.createAttribute(attributename)
40

9

document.createAttribute(attributename)
42

<1

document.createAttribute(attributename)
44

9

document.createAttribute(attributename)
46

<1

document.createAttribute(attributename)
48

9

document.createAttribute(attributename)
10

9

document.createAttribute(attributename)
12

document.createAttribute(attributename)
6<8

document.createAttribute(attributename)
6
document.createAttribute(attributename)
06______97
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2____106____115
document.createAttribute(attributename)
1

document.createAttribute(attributename)
06
document.createAttribute(attributename)
0____165

đầu ra

 

Tạo thuộc tính trong JavaScript là gì?
 

ví dụ 2.  

html




________số 8

<

document.createAttribute(attributename)
0
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2<
document.createAttribute(attributename)
4
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<7
document.createAttribute(attributename)
1

909

<111

9<8

9

document.createAttribute(attributename)
00

<1

document.createAttribute(attributename)
02

9<8

document.createAttribute(attributename)
6
document.createAttribute(attributename)
06____87
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2
document.createAttribute(attributename)
06______14
document.createAttribute(attributename)
1

document.createAttribute(attributename)
2____9____115
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<
document.createAttribute(attributename)
19
document.createAttribute(attributename)
20
document.createAttribute(attributename)
19
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<
document.createAttribute(attributename)
25
document.createAttribute(attributename)
26
document.createAttribute(attributename)
25
document.createAttribute(attributename)
1

document.createAttribute(attributename)
6<46 474649504950
document.createAttribute(attributename)
1

Tạo thuộc tính là gì?

Tạo định nghĩa thuộc tính để lưu trữ thêm thông tin về các đối tượng trong mô-đun . Ví dụ: bạn có thể tạo thuộc tính priority để có thể đặt mức độ ưu tiên cho từng đối tượng trong mô-đun.

Thuộc tính có nghĩa là gì trong JavaScript?

thuộc tính. tập hợp các đối tượng thuộc lớp Attr dựng sẵn, với các thuộc tính tên và giá trị .

Phần tử tạo trong JavaScript là gì?

createElement() được dùng để tạo động nút phần tử HTML có tên đã chỉ định thông qua JavaScript . Phương thức này lấy tên của phần tử làm tham số và tạo nút phần tử đó.

Mục đích của một thuộc tính là gì?

Một thuộc tính xác định một thuộc tính cụ thể của một đối tượng, phần tử hoặc tệp . Nó cũng có thể đề cập đến một giá trị cụ thể cho một phiên bản nhất định của thuộc tính đó.