javascript - 更改移动浏览器的点击事件

标签 javascript html css

我想删除宽度小于 400 像素的浏览器的按钮(温度切换)。相反,我想触摸一个 div 来切换温度。我写了这段代码,但它不起作用。 “touchTemp”是包含的 div 的 ID。

if (window.innerWidth < 400) {
  document.getElementById("touchTemp").addEventListener("click",
    function(e) {
      console.log("Screen width is less than 400px");
      var text = $('#temperature').text();
      if (text.slice(-1) === "F") {
        $("#temperature").html(tempCelsius + " C" + "°");
      } else {
        $("#temperature").html(tempFahrenheit + " F");
      }

      e.preventDefault();
    });
} // end of "if statement"

enter image description here

带轮廓的蓝色 div 是要触摸以切换温度的区域。

CodePen Project Link

最佳答案

为了完整性和便于测试,我建议您只添加触摸处理程序和点击。

var theElement = document.getElementById("touchTemp");

theElement.addEventListener("click", myFunction, false);
theElement.addEventListener("touchend", myFunction, false);

function myFunction(e) {
  console.log("Screen width is less than 400px");
  var text = $('#temperature').text();
  if (text.slice(-1) === "F") {
    $("#temperature").html(tempCelsius + " C" + "°");
  } else {
    $("#temperature").html(tempFahrenheit + " F");
  }

  e.preventDefault();
  return false;
}

关于javascript - 更改移动浏览器的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38955025/

相关文章:

javascript - 为什么我的函数不采用全局变量?

html - 具有两列问题的CSS下拉菜单

Javascript(纯): Set many style attributes using list of values from array

javascript - 如何在除一个 td 之外的 tr 上应用 onClick?

javascript - 如何监听 'after'事件?

html - 使用 Flask 的 CSS 背景图像

css - 表格单元格中的锚定图像未根据单元格宽度调整大小

CSS z-index 999 不适用于 Google Chrome 和 Safari 浏览器

html - 使用CSS向文本扩展/折叠添加过渡

javascript - Vuelidate 是否支持 VueJs 中的 typescript ?