Chúng tôi đã thêm một trình lắng nghe sự kiện
Some text here
Button
0 vào nút, vì vậy một chức năng được gọi mỗi khi nút được nhấpMỗi lần nhấp vào nút, chúng tôi đặt thuộc tính
Some text here
Button
1 thành
Some text here
Button
2 và thay đổi màu nền của trangThay đổi màu nền của phần tử khi nhấp vào #
Để thay đổi màu nền của phần tử khi nhấp chuột
- Thêm trình xử lý sự kiện
0 vào phần tửSome text here
Button - Gán đối tượng
4 cho một biến trong hàmSome text here
Button - Đặt thuộc tính
5 thành màu nền cụ thểSome text here
Button
Mỗi khi nhấp vào nút từ ví dụ, màu nền của chính nút đó sẽ được đặt
Chúng tôi đã sử dụng thuộc tính target trên đối tượng
Some text here
Button
4. Thuộc tính
Some text here
Button
1 là một tham chiếu đến đối tượng [phần tử] mà sự kiện được gửi điNói cách khác,
Some text here
Button
2 cho phép chúng tôi truy cập vào phần tử DOM mà người dùng đã nhấp vàoBạn có thể
Some text here
Button
3 thuộc tính
Some text here
Button
1 để xem phần tử DOM được người dùng nhấp vàoNếu bạn nhấp vào nút và xem đầu ra
Some text here
Button
5 của mình, bạn sẽ thấy phần tử
Some text here
Button
6 đang được ghi lại Hướng dẫn này sẽ thảo luận về cách thay đổi màu nền bằng thuộc tính
Some text here
Button
7 trong JavaScriptThay đổi màu nền bằng thuộc tính
Some text here
Button
7 trong JavaScript
Some text here
Button
Chúng ta có thể thay đổi màu nền bằng thuộc tính
Some text here
Button
7 trong JavaScript. Để sử dụng thuộc tính này, bạn cần lấy phần tử có màu nền mà bạn muốn thay đổi, sau đó bạn có thể sử dụng thuộc tính
Some text here
Button
7 để đặt màu nềnVí dụ: hãy tạo một trang bằng HTML và thay đổi màu nền của phần nội dung thành màu xanh lá cây bằng cách sử dụng thuộc tính
Some text here
Button
7. Xem mã dưới đây
Bạn cũng có thể lấy phần tử có id hoặc tên của lớp. Để lấy một phần tử bằng id của nó, bạn có thể sử dụng hàm
Some text here
Button
2. Để lấy một phần tử bằng cách sử dụng lớp của nó, bạn có thể sử dụng hàm
Some text here
Button
3Ví dụ: hãy lấy một phần tử theo id của nó và thay đổi màu nền của nó bằng thuộc tính
Some text here
Button
7. Xem mã dưới đây
Hello Wold
Đoạn mã trên sẽ chỉ thay đổi màu nền của phần tử có id
Hello Wold
1 chứ không thay đổi toàn bộ phần nội dung. Bạn cũng thay đổi màu nền của một thành phần bằng cách sử dụng mã màu của các màu khác nhau thay vì thay đổi bằng tên màuHãy viết code để thay đổi màu nền của văn bản bằng một nút. Xem mã dưới đây
Change Color
Hello World
Trong mã này, bạn sẽ thấy một nút và một văn bản. Khi bạn nhấn nút, màu nền của văn bản sẽ thay đổi theo màu đã cho
Đưa ra một tài liệu HTML và nhiệm vụ là thay đổi màu nền của tài liệu bằng JavaScript và jQuery.
Cách tiếp cận 1. Cách tiếp cận này sử dụng JavaScript để thay đổi màu nền sau khi nhấp vào nút. Sử dụng HTML DOM Style backgroundColor Property để thay đổi màu nền sau khi nhấp vào nút. Thuộc tính này được sử dụng để thiết lập màu nền của một phần tử.
Thí dụ. Ví dụ này thay đổi màu nền với sự trợ giúp của JavaScript.
html
Some text here
Button
5
Some text here
Button
6
Some text here
Button
7____28
Some text here
Button
9
Some text here
Button
6
Some text here
Button
1
Some text here
Button
8
Some text here
Button
3
Some text here
Button
6______260
Some text here
Button
8
Some text here
Button
62
Some text here
Button
63
Some text here
Button
62
Some text here
Button
65
Some text here
Button
3____267____260
Some text here
Button
8
Some text here
Button
67____31____28
Some text here
Button
9
Some text here
Button
6
Some text here
Button
75
Some text here
Button
76______277
Some text here
Button
78
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26______283
Some text here
Button
76
Some text here
Button
77
Some text here
Button
86
Some text here
Button
8
Some text here
Button
62
Some text here
Button
89
Some text here
Button
3______267____283
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3
Some text here
Button
6
Some text here
Button
97
Some text here
Button
98
Some text here
Button
77
Some text here
Button
60
Some text here
Button
61
Some text here
Button
62
Some text here
Button
63
Some text here
Button
3____267______297
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26______311
Some text here
Button
12
Some text here
Button
77
Some text here
Button
14
Some text here
Button
8
Some text here
Button
62
Some text here
Button
17
Some text here
Button
3____267____311
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26______297
Some text here
Button
98
Some text here
Button
77
Some text here
Button
88
Some text here
Button
89
Some text here
Button
30
Some text here
Button
31
Some text here
Button
30
Some text here
Button
63
Some text here
Button
3____267______297
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26____261
Some text here
Button
8
Some text here
Button
62
Some text here
Button
64
Some text here
Button
62
Some text here
Button
66
Some text here
Button
62
Some text here
Button
68
Some text here
Button
69
Some text here
Button
62
Some text here
Button
601
Some text here
Button
69
Some text here
Button
62
Some text here
Button
604
Some text here
Button
605
Some text here
Button
606
Some text here
Button
62____2608
Some text here
Button
609
Some text here
Button
62
Some text here
Button
611
Some text here
Button
605
Some text here
Button
613
Some text here
Button
605
Some text here
Button
615
Some text here
Button
62
Some text here
Button
617
Some text here
Button
3____267____261
Some text here
Button
8
Some text here
Button
67
Some text here
Button
75
Some text here
Button
8
Some text here
Button
9
Some text here
Button
67
Some text here
Button
7____28đầu ra.
Cách tiếp cận 2. Cách tiếp cận này sử dụng jQuery để thay đổi màu nền sau khi nhấp vào nút
- Phương thức text[] được sử dụng để đặt nội dung văn bản cho phần tử được chọn
- Phương thức on[] được sử dụng làm trình xử lý sự kiện cho các phần tử được chọn và các phần tử con
- Phương thức css[] dùng để thay đổi/thiết lập màu nền của phần tử
Thí dụ. Ví dụ này thay đổi màu nền với sự trợ giúp của JQuery.
html
Some text here
Button
5
Some text here
Button
6
Some text here
Button
7____28
Some text here
Button
9
Some text here
Button
6
Some text here
Button
1
Some text here
Button
8
Some text here
Button
3
Some text here
Button
6______260
Some text here
Button
8
Some text here
Button
62
Some text here
Button
642
Some text here
Button
62____2644
Some text here
Button
3____267____260
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26
Some text here
Button
61
Some text here
Button
653
Some text here
Button
77
Some text here
Button
655
Some text here
Button
8
Some text here
Button
3____267____261
Some text here
Button
8
Some text here
Button
67____31____28
Some text here
Button
9
Some text here
Button
6
Some text here
Button
75
Some text here
Button
76______277
Some text here
Button
78
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26______283
Some text here
Button
76
Some text here
Button
77
Some text here
Button
86
Some text here
Button
8
Some text here
Button
62
Some text here
Button
89
Some text here
Button
3______267____283
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26____297
Some text here
Button
98
Some text here
Button
77
Some text here
Button
60
Some text here
Button
692
Some text here
Button
62
Some text here
Button
63
Some text here
Button
3____267______297
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26____311
Some text here
Button
8
Some text here
Button
62
Some text here
Button
17
Some text here
Button
3____267____311
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26______297
Some text here
Button
98
Some text here
Button
77
Some text here
Button
88
Some text here
Button
89
Some text here
Button
30
Some text here
Button
719
Some text here
Button
30
Some text here
Button
63
Some text here
Button
3____267______297
Some text here
Button
8
Some text here
Button
9
Some text here
Button
3______26____261
Some text here
Button
8
Some text here
Button
62
Some text here
Button
732
Some text here
Button
62
Some text here
Button
734
Some text here
Button
605
Some text here
Button
736
Some text here
Button
605
Some text here
Button
738
Some text here
Button
62
Some text here
Button
740
Some text here
Button
3____267____261
Some text here
Button
8
Some text here
Button
67
Some text here
Button
75
Some text here
Button
8
Some text here
Button
9
Some text here
Button
67
Some text here
Button
7____28đầu ra
JavaScript được biết đến nhiều nhất để phát triển trang web nhưng nó cũng được sử dụng trong nhiều môi trường không có trình duyệt. Bạn có thể học JavaScript từ đầu bằng cách làm theo Hướng dẫn JavaScript và Ví dụ về JavaScript này
jQuery là một thư viện JavaScript mã nguồn mở giúp đơn giản hóa các tương tác giữa một tài liệu HTML/CSS, nó nổi tiếng rộng rãi với triết lý “Viết ít hơn, làm nhiều hơn”. Bạn có thể học jQuery từ đầu bằng cách làm theo Hướng dẫn jQuery và Ví dụ về jQuery này