【问题标题】:Click event not supported on touchscreen触摸屏不支持点击事件
【发布时间】:2021-07-06 06:57:08
【问题描述】:

有没有人知道为什么我的 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 };

Application.js:

import { initButton } from '../components/init-button';
document.addEventListener('turbolinks:load', () => {
  AOS.init({
    offset: 100,
    duration: 2000,
  });
  initButton();
}); 

【问题讨论】:

    标签: javascript ruby-on-rails touch


    【解决方案1】:

    您可能想尝试处理特定于触摸的事件,例如"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() 的使用。这在处理多种事件类型时是必要的,以防止触发相同的回调两次。

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2022-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2011-10-22
      • 1970-01-01
      • 2012-12-13
      相关资源
      最近更新 更多