简体   繁体   English

HTML 按钮在桌面上正常工作 在移动设备上不起作用

[英]HTML buttons work fine on desktop do not work on mobile

I have made a website and while the desktop version is finished i am still fighting with the mobile one.我已经建立了一个网站,虽然桌面版本完成了,但我仍在与移动版本作斗争。 So anyways, every single button that uses js to do its thing doesnt work, while normal buttons with links work just fine.所以无论如何,每一个使用 js 做它的事情的按钮都不起作用,而带有链接的普通按钮工作得很好。 The issue also does not appear on chrome using the 'toggle device toolbar' option, which should technically simulate touch events.该问题也不会出现在使用“切换设备工具栏”选项的 chrome 上,该选项在技术上应该模拟触摸事件。

This is how a sample button looks like:这是示例按钮的外观:

 <a class="button" id="upload-media-button">Upload</a>

And this is the js that uses it:这是使用它的js:

$(document).ready(function() {

    $('#upload-media-button').on('click touchstart', function(){
        dropperForm = document.getElementById("upload-form");
        dropperForm.className = '';
        isUploadFormVisible = 1;
        $("body").addClass("modal-open");

    });
}

Can't say exactly why this is happening.不能确切地说为什么会发生这种情况。

But this may be case.但这可能是这种情况。 We should disable the default behavior of browser.我们应该禁用浏览器的默认行为。

As it is an anchor tag, browser have its own handling.由于它是一个anchor标签,浏览器有自己的处理方式。

Like modify your code like this像这样修改你的代码

$(document).ready(function() {

  $('#upload-media-button').on('click touchstart', function(event){
    event.preventDefault();
    dropperForm = document.getElementById("upload-form");
    dropperForm.className = '';
    isUploadFormVisible = 1;
    $("body").addClass("modal-open");

  });
}

Hope this helps.希望这可以帮助。

try replacing touchstart with tap like this尝试用这样的tap替换touchstart

$(document).ready(function() {

  $('#upload-media-button').on('click tap', function(event){
     event.preventDefault();
     dropperForm = document.getElementById("upload-form");
     dropperForm.className = '';
     isUploadFormVisible = 1;
     $("body").addClass("modal-open");
  });
});

this is assuming that you have included jQuery Mobile in your project.这是假设您在项目中包含 jQuery Mobile。 (which I highly recommend you do). (我强烈建议你这样做)。

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

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