Php xóa phần tử html theo id

Để 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']]; // null
3 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']]; // null
4 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']]; // null
5 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']]; // null
3 sẽ trả về thay vì đưa ra lỗi

Phươ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']]; // null
7

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']]; // null
5 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ố

  1. 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 đặt
  2. const 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']]; // null
4 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']]; // null
2 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']]; // null
4 để 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']]; // null
4 trả về
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
5 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']]; // null
6], 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

  1. bỏ đặt []. Hàm này bỏ đặt một biến đã cho.
    Cú pháp.
    void unset [ mixed $var [, mixed $.. ] ]
  2. 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']]; // null
2

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
4
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
5
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
6

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

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-id
00

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
02

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
04

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
05
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
07

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
09

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
60
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
02

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
64

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
65
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
4____267
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
6
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
02

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
22

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
60
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
65
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
02

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
26

đầ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-id
0

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']]; // null
2

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
4
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
5
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
6

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-id
00

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
02

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
04

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
46
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
6
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
49

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
7

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
09

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
60
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
3
const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.setAttribute['id', 'new-id'];console.log[box.getAttribute['id']]; // new-id
02

const box = document.getElementById['box-1'];console.log[box.getAttribute['id']]; // box-1box.removeAttribute['id'];console.log[box.getAttribute['id']]; // null
26

đầ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-id
6

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

Chủ Đề