Tóm lược. trong hướng dẫn này, bạn sẽ học cách thêm kiểu vào phần tử bằng các phương thức DOM của JavaScript
Giả sử rằng bạn có một phần tử
4 như sauCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Code language: HTML, XML [xml]
JavaScript CSS
Và bạn muốn thêm phong cách cho phần tử này. Bằng cách sử dụng các phương thức JavaScript DOM, bạn có thể thêm kiểu nội tuyến hoặc kiểu toàn cục vào phần tử
kiểu nội tuyến
Để thêm kiểu nội tuyến vào một phần tử, bạn làm theo các bước sau
- Đầu tiên, chọn phần tử bằng cách sử dụng các phương thức DOM như
5. Phần tử được chọn có thuộc tínhconst note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
6 cho phép bạn đặt các kiểu khác nhau cho phần tử
Code language: JavaScript [javascript]const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
- Sau đó, thiết lập các giá trị thuộc tính của đối tượng
0
Code language: JavaScript [javascript]const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Đoạn mã sau thay đổi màu nền và màu của phần tử
4 ở trênCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Code language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
1] Sử dụng thuộc tính cssText
Thêm phong cách cá nhân là khá dài dòng. May mắn thay, bạn có thể thêm tập hợp nhiều kiểu cùng một lúc bằng cách sử dụng thuộc tính
2Code language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Code language: JavaScript [javascript]
note.style.cssText += 'color:red;background-color:yellow';
Trong ví dụ này, chúng tôi đã sử dụng toán tử
3 để nối các kiểu mới vào các kiểu hiện có. Nếu bạn sử dụng toán tửCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
4, kiểu hiện tại sẽ bị xóa, như thế nàyCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Code language: JavaScript [javascript]
note.style.cssText = 'color:red;background-color:yellow';
2] Sử dụng chức năng trợ giúp
Hàm trợ giúp sau chấp nhận một phần tử và một đối tượng kiểu. Nó thêm tất cả các kiểu từ đối tượng
0 vào thuộc tínhCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
0 của
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Code language: JavaScript [javascript]
7Code language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
1Code language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
Và bạn có thể sử dụng hàm trợ giúp
8 này để đặt kiểu cho phần tửCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
4 như sauCode language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
3Code language: JavaScript [javascript]
const note = document.querySelector['.note']; note.style.backgroundColor = 'yellow'; note.style.color = 'red';
phong cách toàn cầu
Để thêm các kiểu toàn cầu vào một phần tử, bạn tạo phần tử kiểu, điền vào đó các quy tắc CSS và nối phần tử kiểu vào cây DOM, như thế này