TL; DR: Không, bạn không thể làm điều này theo bất kỳ cách nào được hỗ trợ tự nhiên.: No, you cannot do this in any natively supported way.
Cách duy nhất tôi biết để đạt được điều này là tạo một đối tượng lưu trữ tùy chỉnh nơi bạn giữ một bản ghi của người nghe được thêm vào. Một cái gì đó dọc theo các dòng sau:
/* Create a storage object. */
var CustomEventStorage = [];
Bước 1: Đầu tiên, bạn sẽ cần một hàm có thể đi qua đối tượng lưu trữ và trả về bản ghi của một phần tử được đưa ra phần tử [hoặc sai]. First, you will need a function that can traverse the storage object and return the record of an element given the element [or false].
/* The function that finds a record in the storage by a given element. */
function findRecordByElement [element] {
/* Iterate over every entry in the storage object. */
for [var index = 0, length = CustomEventStorage.length; index < length; index++] {
/* Cache the record. */
var record = CustomEventStorage[index];
/* Check whether the given element exists. */
if [element == record.element] {
/* Return the record. */
return record;
}
}
/* Return false by default. */
return false;
}
Bước 2: Sau đó, bạn sẽ cần một chức năng có thể thêm trình nghe sự kiện nhưng cũng chèn trình nghe vào đối tượng lưu trữ. Then, you will need a function that can add an event listener but also insert the listener to the storage object.
/* The function that adds an event listener, while storing it in the storage object. */
function insertListener [element, event, listener, options] {
/* Use the element given to retrieve the record. */
var record = findRecordByElement[element];
/* Check whether any record was found. */
if [record] {
/* Normalise the event of the listeners object, in case it doesn't exist. */
record.listeners[event] = record.listeners[event] || [];
}
else {
/* Create an object to insert into the storage object. */
record = {
element: element,
listeners: {}
};
/* Create an array for event in the record. */
record.listeners[event] = [];
/* Insert the record in the storage. */
CustomEventStorage.push[record];
}
/* Insert the listener to the event array. */
record.listeners[event].push[listener];
/* Add the event listener to the element. */
element.addEventListener[event, listener, options];
}
Bước 3: Liên quan đến yêu cầu thực tế của câu hỏi của bạn, bạn sẽ cần chức năng sau để kiểm tra xem một phần tử đã được thêm một trình nghe sự kiện cho một sự kiện được chỉ định. As regards the actual requirement of your question, you will need the following function to check whether an element has been added an event listener for a specified event.
/* The function that checks whether an event listener is set for a given event. */
function listenerExists [element, event, listener] {
/* Use the element given to retrieve the record. */
var record = findRecordByElement[element];
/* Check whether a record was found & if an event array exists for the given event. */
if [record && event in record.listeners] {
/* Return whether the given listener exists. */
return !!~record.listeners[event].indexOf[listener];
}
/* Return false by default. */
return false;
}
Bước 4: Cuối cùng, bạn sẽ cần một chức năng có thể xóa trình nghe khỏi đối tượng lưu trữ. Finally, you will need a function that can delete a listener from the storage object.
/* The function that removes a listener from a given element & its storage record. */
function removeListener [element, event, listener, options] {
/* Use the element given to retrieve the record. */
var record = findRecordByElement[element];
/* Check whether any record was found and, if found, whether the event exists. */
if [record && event in record.listeners] {
/* Cache the index of the listener inside the event array. */
var index = record.listeners[event].indexOf[listener];
/* Check whether listener is not -1. */
if [~index] {
/* Delete the listener from the event array. */
record.listeners[event].splice[index, 1];
}
/* Check whether the event array is empty or not. */
if [!record.listeners[event].length] {
/* Delete the event array. */
delete record.listeners[event];
}
}
/* Add the event listener to the element. */
element.removeEventListener[event, listener, options];
}
Snippet:
Mặc dù hơn 5 năm đã trôi qua kể từ khi OP đăng câu hỏi, tôi tin rằng những người vấp ngã trong tương lai sẽ được hưởng lợi từ câu trả lời này, vì vậy hãy thoải mái đưa ra đề xuất hoặc cải tiến cho nó. 😊
Khi chuyển các giá trị tham số, hãy sử dụng "hàm ẩn danh" gọi hàm được chỉ định với các tham số:
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
Element.AdDeventListener ["Nhấp", myFunction];
Hãy tự mình thử »
function myFunction [] {& nbsp; & nbsp; alert ["hello world!"];}
Thêm nhiều trình xử lý sự kiện vào cùng một phần tử
Phương thức addEventListener[]
cho phép bạn thêm nhiều sự kiện vào cùng một yếu tố, mà không ghi đè lên các sự kiện hiện có:
Element.AdDeventListener ["Nhấp", myFunction]; Element.AdDeventListener ["Nhấp", MySecondFunction];
Bạn có thể thêm trình nghe sự kiện vào bất kỳ đối tượng DOM nào không chỉ các phần tử HTML. tức là đối tượng cửa sổ.
Phương pháp addEventListener[]
giúp việc kiểm soát sự kiện phản ứng dễ dàng hơn như thế nào.
Khi sử dụng phương thức addEventListener[]
, JavaScript được tách ra khỏi đánh dấu HTML, để có khả năng đọc tốt hơn và cho phép bạn thêm trình nghe sự kiện ngay cả khi bạn không kiểm soát đánh dấu HTML.
Bạn có thể dễ dàng xóa trình nghe sự kiện bằng cách sử dụng phương thức removeEventListener[]
.
Cú pháp
Element.AddeventListener [sự kiện, chức năng, usecapture];
Tham số đầu tiên là loại sự kiện [như "
/* The function that finds a record in the storage by a given element. */
function findRecordByElement [element] {
/* Iterate over every entry in the storage object. */
for [var index = 0, length = CustomEventStorage.length; index < length; index++] {
/* Cache the record. */
var record = CustomEventStorage[index];
/* Check whether the given element exists. */
if [element == record.element] {
/* Return the record. */
return record;
}
}
/* Return false by default. */
return false;
}
0" hoặc "/* The function that finds a record in the storage by a given element. */
function findRecordByElement [element] {
/* Iterate over every entry in the storage object. */
for [var index = 0, length = CustomEventStorage.length; index < length; index++] {
/* Cache the record. */
var record = CustomEventStorage[index];
/* Check whether the given element exists. */
if [element == record.element] {
/* Return the record. */
return record;
}
}
/* Return false by default. */
return false;
}
1" hoặc bất kỳ sự kiện HTML DOM nào khác.]Tham số thứ hai là chức năng chúng tôi muốn gọi khi sự kiện xảy ra.
Tham số thứ ba là giá trị boolean chỉ định xem có sử dụng sự kiện sủi bọt hay sự kiện. Tham số này là tùy chọn.
Lưu ý rằng bạn không sử dụng tiền tố "ON" cho sự kiện; Sử dụng "
/* The function that finds a record in the storage by a given element. */
function findRecordByElement [element] {
/* Iterate over every entry in the storage object. */
for [var index = 0, length = CustomEventStorage.length; index < length; index++] {
/* Cache the record. */
var record = CustomEventStorage[index];
/* Check whether the given element exists. */
if [element == record.element] {
/* Return the record. */
return record;
}
}
/* Return false by default. */
return false;
}
0" thay vì "/* The function that finds a record in the storage by a given element. */
function findRecordByElement [element] {
/* Iterate over every entry in the storage object. */
for [var index = 0, length = CustomEventStorage.length; index < length; index++] {
/* Cache the record. */
var record = CustomEventStorage[index];
/* Check whether the given element exists. */
if [element == record.element] {
/* Return the record. */
return record;
}
}
/* Return false by default. */
return false;
}
3".Thêm trình xử lý sự kiện vào một phần tử
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
Element.AdDeventListener ["Nhấp", function [] {alert ["Hello World!"];}];
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
Element.AdDeventListener ["Nhấp", function [] {alert ["Hello World!"];}];
Hãy tự mình thử »
alert ["Hello World!"];
}
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Element.AdDeventListener ["Nhấp", myFunction];
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
element.addEventListener["click", mySecondFunction];
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
element.addEventListener["click", mySecondFunction];
element.addEventListener["mouseout", myThirdFunction];
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Element.AdDeventListener ["Nhấp", myFunction];
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
Element.AdDeventListener ["Nhấp", function [] {alert ["Hello World!"];}];
document.getElementById["demo"].innerHTML = sometext;
}];
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Element.AdDeventListener ["Nhấp", myFunction];
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Element.AdDeventListener ["Nhấp", myFunction];
function myFunction [] {& nbsp; & nbsp; alert ["hello world!"];}
Thêm nhiều trình xử lý sự kiện vào cùng một phần tử
Phương thức addEventListener[]
cho phép bạn thêm nhiều sự kiện vào cùng một yếu tố, mà không ghi đè lên các sự kiện hiện có:
Element.AdDeventListener ["Nhấp", myFunction]; Element.AdDeventListener ["Nhấp", MySecondFunction];
Bạn có thể thêm các sự kiện thuộc các loại khác nhau vào cùng một phần tử:useCapture];
Element.AddeventListener ["Mouseover", MyFunction]; Element.AdDeventListener ["Nhấp", MySecondFunction]; Element.AddeventListener ["Mouseout", Chuyện truyền thuyết];
Thí dụ
Thông báo "Xin chào Thế giới!" Khi người dùng nhấp vào một phần tử:
document.getElementById["myDiv"].addEventListener["click",
myFunction, true];
Hãy tự mình thử »
Bạn cũng có thể tham khảo chức năng "có tên" bên ngoài:
Element.AdDeventListener ["Nhấp", myFunction];
function myFunction [] {& nbsp; & nbsp; alert ["hello world!"];}
Thêm nhiều trình xử lý sự kiện vào cùng một phần tử