简体   繁体   English

onclick 和 ontouchstart 不适用于任何元素的移动设备

[英]onclick and ontouchstart not working on mobile for any element

I have been working on a website and I use onclick to open the navigation but when I tried it on mobile it didn't work, it just did the:hover animation.我一直在网站上工作,我使用 onclick 打开导航,但是当我在移动设备上尝试它时它不起作用,它只是做了:hover animation。 I then added ontouchstart to the div and it still doesn't work on mobile.然后我将 ontouchstart 添加到 div 中,但它仍然无法在移动设备上运行。 When I tap it it acts like I hover over it.当我点击它时,它就像我在它上面的 hover 一样。 I've looked at a few articles on why it might not work but I can't figure it out.我看过一些关于为什么它可能不起作用的文章,但我无法弄清楚。 Also any element that uses onclick doesn't work on mobile.此外,任何使用 onclick 的元素都无法在移动设备上使用。 Also the js is a separate file (not sure if this would for some reason affect it). js也是一个单独的文件(不确定这是否会由于某种原因影响它)。

<div id="navWrap" onclick="openNav()" ontouchstart="openNav()">
        <div class="navLine"></div>
        <div class="navLine" id="navMid"></div>
        <div class="navLine" id="navBottom"></div>
</div>
function openNav() {
  document.getElementById("mySidenav").style.width = "100%";
}

Edit: More code编辑:更多代码

<div id="headWrap">
    <div id="logoWrap"><img src="/static/images/logo.svg" id="logo"></div>
    <div id="headText">Cite Chef</div>
    <div id="navWrap" onclick="openNav()" ontouchstart="openNav()">
        <div class="navLine"></div>
        <div class="navLine" id="navMid"></div>
        <div class="navLine" id="navBottom"></div>
    </div>
</div>
window.addEventListener('load', function(){
    document.getElementById('navWrap').addEventListener('mousedown', openNav);
    document.getElementsByClassName('closebtn')[0].addEventListener('click', closeNav);
    document.getElementById('Create').addEventListener('click', create);
    document.getElementById('TInput').addEventListener('click', MYfunctionTwo);
});

This might be due to some overlapping div's or element on the phone view, you need not add a separate event listener for the phone view click.这可能是由于电话视图上有一些重叠的 div 或元素,您不需要为电话视图单击添加单独的事件侦听器。

Try to inspect the code in the mobile view you might some overlapping element.尝试检查移动视图中的代码,您可能会发现一些重叠元素。

As you have mentioned on your previous comment, you are testing this on Safari.正如您在之前的评论中提到的,您正在 Safari 上进行测试。 The inline onclick events are going to cause issues in your Web App.内联onclick事件将导致您的 Web 应用程序出现问题。 This is not supported in some browsers.这在某些浏览器中不受支持。 I recommend using addEventListener .我建议使用addEventListener Simply remove all the inline events and migrate event handling implementations to addEventListener.只需删除所有内联事件并将事件处理实现迁移到 addEventListener。

<div id="headWrap">
    <div id="logoWrap"><img src="/static/images/logo.svg" id="logo"></div>
    <div id="headText">Cite Chef</div>
    <div id="navWrap">
        <div class="navLine"></div>
        <div class="navLine" id="navMid"></div>
        <div class="navLine" id="navBottom"></div>
    </div>
</div>

document.getElementById('navWrap').addEventListener('click', openNav);

In case you are using a lower version of Safari Mobile.如果您使用的是较低版本的 Safari Mobile。 This is good information to have from MDN Web Docs in case you plan on doing event delegation in the future:这是 MDN Web Docs 提供的很好的信息,以防您将来计划进行事件委托:

Safari Mobile 7.0+ (and likely earlier versions too) suffers from a bug where click events aren't fired on elements that aren't typically interactive (eg <div> ) and which also don't have event listeners directly attached to the elements themselves (ie event delegation is being used). Safari Mobile 7.0+(也可能是更早的版本)存在一个错误,即点击事件不会在通常不具有交互性的元素上触发(例如<div> )并且也没有事件侦听器直接附加到元素他们自己(即正在使用事件委托)。

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event参考: https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM