有谁知道为什么我的 JS 功能在手机上不起作用?它在桌面上完美运行,但在 Iphone/Android 手机上没有响应。 我需要添加一些东西以便手机可以理解吗?
init-button.js :
const initButton = () => {
const toggleBtn = document.querySelector('#toggleBtn');
const divList = document.querySelectorAll('.discover');
toggleBtn.addEventListener('click', () => {
divList.forEach((item) => {
item.classList.add('block');
});
});
};
export { initButton };
应用程序.js:
import { initButton } from '../components/init-button';
document.addEventListener('turbolinks:load', () => {
AOS.init({
offset: 100,
duration: 2000,
});
initButton();
});
答案 0 :(得分:0)
您可能想尝试处理特定于触摸的事件,例如 "touchend"
。有关详细信息,请参阅 this article。以下是从那篇文章中借用的一个简单示例:
var theElement = document.getElementById("theElement");
theElement.addEventListener("mouseup", tapOrClick, false);
theElement.addEventListener("touchend", tapOrClick, false);
function tapOrClick(event) {
//handle tap or click.
event.preventDefault();
return false;
}
特别要注意 event.preventDefault()
的使用。这在处理多个事件类型时是必要的,以防止两次触发相同的回调。