jQuery ContextMenu事件在IOS 8.2中不起作用

时间:2015-03-24 09:28:19

标签: javascript jquery ios contextmenu

我在.html示例中使用了contextMenu事件,当我长按DIV时会触发它,但是现在它无法正常工作。在最新的IOS 8.2版本中有什么问题。以下是示例代码

<head>
    <title></title>
    <script src="Scripts/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            $("#content").on("contextmenu", function () {
                alert("CM");
            });
        });
    </script>
</head>

<body>
    <div id="content" style="height:300px; width:300px; background-color:gray;"></div>
</body>

这是工作样本

http://jsfiddle.net/4zu1ckgg/

请有人帮助我。

1 个答案:

答案 0 :(得分:3)

基本上,在iOS上,触摸事件不会被模拟为鼠标事件。 请改用触摸事件:&#34; touchstart&#34;,&#34; touchmove&#34;和&#34; touchend&#34;。

在你的情况下,在iOS上,与Android相反,&#34; contextmenu&#34;长时间触摸屏幕时不会触发。 要在iOS上自定义长时间触摸,您应该使用以下内容:

// Timer for long touch detection
var timerLongTouch;
// Long touch flag for preventing "normal touch event" trigger when long touch ends
var longTouch = false;

$(touchableElement)
  .on("touchstart", function(event){
      // Prevent default behavior
      event.preventDefault();
      // Test that the touch is correctly detected
      alert("touchstart event");
      // Timer for long touch detection
      timerLongTouch = setTimeout(function() {
          // Flag for preventing "normal touch event" trigger when touch ends. 
          longTouch = true;
          // Test long touch detection (remove previous alert to test it correctly)
          alert("long mousedown");
      }, 1000);
  })
  .on("touchmove", function(event){
      // Prevent default behavior
      event.preventDefault();
      // If timerLongTouch is still running, then this is not a long touch 
      // (there is a move) so stop the timer
      clearTimeout(timerLongTouch);

      if(longTouch){
          longTouch = false;
          // Do here stuff linked to longTouch move
      } else {
          // Do here stuff linked to "normal" touch move
      }
  })
  .on("touchend", function(){
      // Prevent default behavior
      event.preventDefault();
      // If timerLongTouch is still running, then this is not a long touch
      // so stop the timer
      clearTimeout(timerLongTouch);

      if(longTouch){
          longTouch = false;
          // Do here stuff linked to long touch end 
          // (if different from stuff done on long touch detection)
      } else {
          // Do here stuff linked to "normal" touch move
      }
  });

这是一个解释(以及其他)触摸事件未在每个操作系统上模拟为鼠标事件的页面:http://www.html5rocks.com/en/mobile/touchandmouse/

希望这会有所帮助,我花了很长时间才弄明白;)