Phương thức HTML DOM removeEventListener ()
❮ Đối tượng phần tửThí dụ
Xóa sự kiện "mousemove" đã được đính kèm với phương thức addEventListener ():
// Attach an event handler to <div>
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
// Remove the event handler from <div>
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
Định nghĩa và Cách sử dụng
Phương thức removeEventListener () loại bỏ một trình xử lý sự kiện đã được đính kèm với phương thức addEventListener () .
Lưu ý: Để loại bỏ trình xử lý sự kiện, hàm được chỉ định với phương thức addEventListener () phải là một hàm bên ngoài, giống như trong ví dụ trên (myFunction).
Các hàm ẩn danh, như " element .removeEventListener (" event ", function () { myScript });" sẽ không làm việc.
Hỗ trợ trình duyệt
Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ phương pháp này.
Method | |||||
---|---|---|---|---|---|
removeEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
Cú pháp
element.removeEventListener(event, function, useCapture)
Giá trị tham số
Parameter | Description |
---|---|
event | Required. A String that specifies the name of the event to remove. Note: Do not use the "on" prefix. For example, use "click" instead of "onclick". Tip: For a list of all HTML DOM events, look at our complete HTML DOM Event Object Reference. |
function | Required. Specifies the function to remove. |
useCapture |
Optional. A Boolean value that specifies the event phase
to remove the event handler from. Possible values:
|
Chi tiết kỹ thuật
Phiên bản DOM: | Sự kiện DOM cấp 2 |
---|---|
Giá trị trả lại: | Không có giá trị trả lại |
Changelog: | Tham số useCapture trở thành tùy chọn trong Firefox 6 và Opera 12.0 (luôn là tùy chọn cho Chrome, IE và Safari) |
Các ví dụ khác
Thí dụ
Đối với các trình duyệt không hỗ trợ phương thức removeEventListener (), bạn có thể sử dụng phương thức detachEvent ().
Ví dụ này minh họa một giải pháp trình duyệt chéo:
var x = document.getElementById("myDIV");
if (x.removeEventListener) { // For all major browsers, except IE 8 and earlier
x.removeEventListener("mousemove", myFunction);
} else if (x.detachEvent) { // For IE 8 and earlier versions
x.detachEvent("onmousemove", myFunction);
}
Các trang liên quan
Hướng dẫn JavaScript: HTML DOM EventListener
Tham khảo JavaScript: document.removeEventListener ()
❮ Đối tượng phần tử