【问题标题】:Click event on span element works on desktop, but not mobile (jquery)span 元素上的单击事件适用于桌面,但不适用于移动设备(jquery)
【发布时间】:2018-02-04 02:00:32
【问题描述】:

span 元素上的 Click 事件会在桌面设备上触发,但不会在移动设备上触发。跨度是用户单击“获取天气”按钮后填充的当前温度旁边的温度单位。

这是与跨度点击事件相关的代码部分:

$(document).on('click', "span" , function(){
    var str=document.getElementById("tempUnits").innerHTML;
    if (str==="Celsius"){
      document.getElementById("tempUnits").innerHTML="Fahrenheit";
      $("#temp").html(tempC + '<span id="tempUnitsSpan"><strong> C </strong></span>' + " and " + weatherDescription);
    }

    else if (str==="Fahrenheit"){
      document.getElementById("tempUnits").innerHTML="Celsius";
          tempF=Math.max( Math.round((tempC*1.8+32) * 10) / 10, 2.8 ).toFixed(2);
        console.log(tempF);

      $("#temp").html(tempF + '<span id="tempUnitsSpan"><strong> F  </strong></span>' + " and " + weatherDescription);
    }

page

page w/ code

【问题讨论】:

  • 在手机上点击两次能用吗?
  • @messerbill(不,用原始代码点击两次后不起作用)

标签: javascript jquery mobile responsive


【解决方案1】:

我猜如果在移动设备上点击该元素两次,它就会起作用。为了解决这个问题,添加一个 touchstart 事件。当触摸点放置在触摸表面上时触发 touchstart 事件。

$(document).on('click touchstart', "span" , function(){
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2013-11-22
    • 2021-04-09
    • 1970-01-01
    • 2017-03-16
    相关资源
    最近更新 更多