Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript onclick&;ontouch_Javascript_Html - Fatal编程技术网

javascript onclick&;ontouch

javascript onclick&;ontouch,javascript,html,Javascript,Html,我是Javascript新手,不知道是否有人能告诉我如何正确添加“ontouch”函数。 我有一个html onclick函数,在浏览器中可以正常工作,但在iPhone上不行: <a href="javascript:void(0);" style="font-size:12px; color: #f2f2f2;" class="icon" onclick="myFunction()">&#9776;</a> 在这里搜索,人们建议添加如下内容: // First

我是Javascript新手,不知道是否有人能告诉我如何正确添加“ontouch”函数。 我有一个html onclick函数,在浏览器中可以正常工作,但在iPhone上不行:

<a href="javascript:void(0);" style="font-size:12px; color: #f2f2f2;" class="icon" onclick="myFunction()">&#9776;</a>
在这里搜索,人们建议添加如下内容:

// 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);
但是什么去哪里,怎么去

这可能对你有帮助

$('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
}
HTML:


你有没有尝试过这个建议,并看到它奏效了?我的意思是,现在还不清楚你是在问正确的方法还是你共享的最后一个代码块的gebuinity。它有30个投票,所以它显然对某些人有效。但我要求学习。我想解释一下第三个代码。javascript代码中的内容和原因。这就是我感兴趣的。谢谢。但是如果按钮不是被点击而是被触摸呢?这是我真正的问题。如何使按钮既可点击又可触摸。按钮总是既可点击又可触摸。onclick功能可用于触摸屏和左键点击。哇。我刚让一个朋友试过,你是对的。我的手机显然坏了,谷歌告诉我还有一个功能我必须使用。很抱歉给您带来不便。
$('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
}
<a href="javascript:void(0);" style="font-size:12px; color: #f2f2f2;" class="icon" onclick="myFunction()">&#9776;</a>
function myFunction() {

  // the code that need to be executed when button is clicked

}