我并不是Javascript方面的专家,但是我似乎遇到了移动设备上onclick函数普遍存在的问题。Onclick需要鼠标操作,而在手机上这并不适用。在Jquery中,你可以使用"on"函数来解决这个问题,但是在普通的Javascript中该怎么做呢?
// Get the modal
var modal = document.getElementById('reserveer-modal');
// Get the button that opens the modal
var btn = document.getElementById("reserveer-knop");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
var x = window.innerWidth;
if (x > 768) {
//event.preventDefault();
modal.style.display = "block";
} else {
//event.preventDefault();
}
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
el.addEventListener("touchstart", handleStart)
- Dan