Để xóa thuộc tính ID khỏi một phần tử, hãy gọi phương thức
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3 trên phần tử đó, chuyển chuỗi
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4 làm đối số
Ví dụ
mục lục. html
Removing the ID Attribute from an Element with
JavaScript
This is a box
Đây là cách chúng tôi có thể xóa ID khỏi phần tử
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null5 mà chúng tôi đã tạo
mục lục. js
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
Phương thức Element removeAttribute[] xóa thuộc tính của một phần tử có tên được chỉ định
Ghi chú. Nếu thuộc tính được chỉ định không tồn tại,
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3 sẽ trả về thay vì đưa ra lỗiPhương thức phần tử setAttribute[]
Nếu bạn muốn thay thế ID của phần tử thay vì xóa nó, bạn có thể sử dụng phương thức
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
Ví dụ: chúng ta có thể thay đổi ID của phần tử
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null5 mà chúng ta đã tạo trước đó thành một giá trị mới
Ví dụ
mục lục. js
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
Phương thức Element setAttribute[] nhận hai đối số
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
9. Một chuỗi chỉ định tên của thuộc tính có giá trị được đặtconst box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
0. Một chuỗi chứa giá trị để gán cho thuộc tính
Chúng tôi chuyển
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4 và id mới lần lượt làm đối số thứ nhất và thứ hai để đặt thuộc tính ID
Ghi chú. Nếu thuộc tính đã tồn tại trên phần tử, giá trị được cập nhật. Mặt khác, một thuộc tính mới được thêm vào với tên và giá trị được chỉ định
Phương thức getAttribute[] của phần tửChúng tôi đã sử dụng phương thức getAttribute[] một số lần trong bài viết này. Gọi phương thức này trên
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null2 trả về giá trị của thuộc tính với tên đã chỉ định. Vì vậy, chúng tôi gọi nó bằng
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4 để lấy ID của phần tử
Ghi chú. Nếu thuộc tính không tồn tại trên phần tử,
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4 trả về
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null5 hoặc một chuỗi rỗng [
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null6], tùy thuộc vào trình duyệt
Để xóa phần tử khỏi mảng, chúng ta có thể sử dụng hàm unset[] để xóa phần tử khỏi mảng và sau đó sử dụng hàm array_values[] tự động lập chỉ mục cho mảng theo số
Chức năng được sử dụng
- bỏ đặt []. Hàm này bỏ đặt một biến đã cho.
Cú pháp.void unset [ mixed $var [, mixed $.. ] ]
- array_values[]. Hàm này trả về tất cả các giá trị từ mảng và lập chỉ mục mảng theo số.
Cú pháp.array array_values [ array $array ]
ví dụ 1
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null2
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null5
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null6
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
array array_values [ array $array ]0
array array_values [ array $array ]1____92
array array_values [ array $array ]3
array array_values [ array $array ]0
array array_values [ array $array ]5
array array_values [ array $array ]2
array array_values [ array $array ]7
array array_values [ array $array ]0
array array_values [ array $array ]1
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id00
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id02
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id04
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id05
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id07
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id09
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id60
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id02
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id64
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id65
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4____267
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null6
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id02
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null22
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id60
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id65
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id02
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null26
đầu ra.
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id0
Chúng ta cũng có thể sử dụng hàm array_splice[] để loại bỏ một phần của mảng và thay thế bằng một phần khác.
Ví dụ 2.
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null2
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null4
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null5
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null6
array array_values [ array $array ]0
array array_values [ array $array ]1____92
array array_values [ array $array ]3
array array_values [ array $array ]0
array array_values [ array $array ]5
array array_values [ array $array ]2
array array_values [ array $array ]7
array array_values [ array $array ]0
array array_values [ array $array ]1
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id00
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id02
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id04
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null46
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null6
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null49
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null7
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id09
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id60
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id02
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null26
đầu ra.
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id6
PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này