Để hiển thị hoặc ẩn một Trình nghe Ví dụ, giả sử bạn có một phần tử HTML Phần tử Bạn cần thêm trình xử lý sự kiện HTML sẽ được hiển thị như thể phần tử Khi bạn nhấp lại vào phần tử Vì giải pháp này đang sử dụng API JavaScript gốc của trình duyệt nên bạn không cần cài đặt bất kỳ thư viện JavaScript nào như jQuery, bạn có thể thêm trình lắng nghe sự kiện
title
vào phần tử title
cho nút sẽ có một function
sẽ thay đổi thuộc tính display
của
1] thành const targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
2const targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
3 như sauconst targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
This is the FIRST div
This is the SECOND div
This is the THIRD div
Hide THIRD div
ở trên được tạo để ẩn hoặc hiển thị phần tử
5 khi nhấp chuộtconst targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
title
vào phần tử như thế này
const targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
display
thành
2const targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
, thuộc tính
display
sẽ được đặt trở lại thành
1, do đó, const targetDiv = document.getElementById["third"];
const btn = document.getElementById["toggle"];
btn. title = function [] {
if [targetDiv.style.display !== "none"] {
targetDiv.style.display = "none";
} else {
targetDiv.style.display = "block";
}
};
Chủ Đề
Cách ẩn nút sau khi nhấp vào trong JavaScript
bằng một cú nhấp chuột
từ giá trị mặc định [là
chưa từng tồn tại bằng cách đặt thuộc tính
sẽ được hiển thị lại trong trang HTML