Click event listener works in Safari in OSX but not in iOS

前端 未结 3 1501
栀梦
栀梦 2021-01-13 11:46

I\'m making a web app and I want to click on an element and handle the click in one long click event handler. I\'m testing in Safari. The following works fine in Safari on m

相关标签:
3条回答
  • 2021-01-13 12:08

    Try changing the event listener "click" to "click touchstart"

    0 讨论(0)
  • 2021-01-13 12:10

    This code should work cross-browser:

    function Subscribe(event, element, func) {
        if (element.addEventListener) {
            element.addEventListener(event, func, false);
        } else if (element.attachEvent) {
            element.attachEvent("on" + event, func);
        } else {
            element['on' + event] = func;
        }
    }
    
    function func () {
        alert('hi');
    }
    
    Subscribe('click', window, func);
    
    0 讨论(0)
  • 2021-01-13 12:29

    I found a very simple solution. I put another div about the div element in my sample code (see my question above). The opening div tag is

    <div onClick="">
    

    All divs inside this div tag will now trigger the click event. This also works with touchstart.

    0 讨论(0)
提交回复
热议问题