[我正在尝试创建Onclick函数,我的最终目标是让onclick告知GPS位置。我了解功能的基本思想(“函数” name“()”)我已经在HTML中创建了一个按钮文件。我想我需要在render.js中做这个功能吗?我不知道如何使它们一起工作。我是编程的新手。 (从今天开始)另一件事,我正在使用一种叫做电子的东西来尝试做到这一点。如果您对使用更好的产品有任何建议,我很乐意提出建议。
注意:我实际上是出于学习目的而这样做。
预先感谢
您不需要电子来做到这一点。浏览器内置了此功能。这是一个演示:
<button class="alert-location">Get Location</button>
document.querySelector(".alert-location").addEventListener("click", () => {
navigator.geolocation.getCurrentPosition(({coords}) => {
alert(`Latitude: ${coords.latitude}, Longitude: ${coords.longitude}`)
}, (error) => {
alert(`Error: ${error}`)
})
})