按钮 onClick 在移动设备上不起作用

Button onClick not working on mobile device

我尝试了一大堆东西:
onClick not working on mobile (touch)

document .click function for touch device

然而似乎没有任何效果。

这是我的:

#modal-close {cursor:pointer;}

<button class="button" id="modal-close">ok</button>

function hideModal(){
    var modal = document.getElementById('modal');
    modal.style.display = 'none';
}

 $(function() {
        $('#modal-close').on('click touchstart',function (e) {
            hideModal();
            })
        });

我可能做错了什么?

将按钮的 ID 更改为 modal_close。这可能对你有用。 id 值就像一个标识符,所以有规则我们不能使用每个符号来命名标识符。

结果

 #modal-close {cursor:pointer;}

<button class="button" id="modal-close">ok</button>

function hideModal(){
    var modal = document.getElementById('modal');
    modal.style.display = 'none';
}

 $(function() {
        $('#modal-close').on('click touchstart',function (e) {
            hideModal();
            })
        });

有效

让我着迷的是 hideModal(),它使用 classList 添加或删除 类。我将其切换为 className += 并且它按预期工作。