[英]Change click event for mobile browsers
我想為寬度小於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"
藍色div框是要觸摸以切換溫度的區域。
我建議您將觸摸處理程序與單擊一起添加,以確保完整性和易於測試性。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.