我是Javascript的新手,我想知道是否有人可以告诉我' ontouch'应该以正确的方式添加函数。 我有这个html onclick函数,在browswers中工作正常,但在iPhone上没有:
<a href="javascript:void(0);" style="font-size:12px; color: #f2f2f2;" class="icon" onclick="myFunction()">☰</a>
和javascript部分:
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
在这里搜索人们建议添加这样的东西:
// First we check if you support touch, otherwise it's click:
let touchEvent = 'ontouchstart' in window ? 'touchstart' : 'click';
// Then we bind via thát event. This way we only bind one event, instead of the two as below
document.getElementById('hbs').addEventListener(touchEvent, someFunction);
// or if you use jQuery:
$('#hbs').on(touchEvent, someFunction);
但是在哪里以及如何?
答案 0 :(得分:0)
它可能对你有帮助。
$('myTopnav').bind('touchstart' , function(e) {
// get x,y code
var touch = e.touches[0];
var y = touch.pageY;
var x = touch.pageX;
// pass to function down(x,y);
down(x,y);
});
function down(x,y) {
// do something with x,y
}
答案 1 :(得分:0)
HTML:
<a href="javascript:void(0);" style="font-size:12px; color: #f2f2f2;" class="icon" onclick="myFunction()">☰</a>
JS:
function myFunction() {
// the code that need to be executed when button is clicked
}