Uncaught TypeError mapon is not a function
The error message "Uncaught TypeError: map.on is not a function" typically occurs when you try to use the .on() method on an object that is not a valid event emitter.
To resolve this error, you should verify that you are using the correct object and ensure that it supports the .on() method. Common scenarios where this error occurs are:
-
Using
.on()on a plain JavaScript object: The.on()method is not a built-in method for plain JavaScript objects. If you want to add event listeners to a regular object, you can use theaddEventListenermethod instead. -
Using
.on()on a DOM element that doesn't support it: Make sure you are using the correct DOM element that supports the.on()method. For example, if you are trying to add an event listener to a<div>element, you should use the.addEventListener()method instead. -
Using
.on()on an incorrect library or API object: If you are using a library or API, ensure that you have correctly initialized and imported it. Double-check the documentation to verify the correct usage of event listeners.
Here's an example of adding an event listener to a DOM element using the correct approach:
const map = document.getElementById('map');
map.addEventListener('click', function() {
console.log('Map clicked!');
});
In this example, map is a DOM element, and we use addEventListener() to attach a click event listener to it
原文地址: https://www.cveoy.top/t/topic/hVzK 著作权归作者所有。请勿转载和采集!