jQuery点击事件无法在iPhone浏览器上运行(已经尝试过光标:指针;)

问题描述 投票:0回答:2

我想在我的wordpress网站上的锚标签上设置一个点击事件。除了我的iPhone之外,我能够在我的所有设备(PC,Android手机,平板电脑)上使用它。

我已经尝试将'Cursor'设置为'Pointer'以获得锚标签无效。

我已经尝试了所有我能在SO上找到的东西,似乎没有任何效果:/

任何帮助,将不胜感激。

$("a[href^=#]").on('click touchstart', function(e) {
      e.preventDefault(); var dest = $(this).attr('href');
      console.log(dest);
      $('html,body').animate({ scrollTop: $(dest).offset().top }, 'slow');
    });
.scroller {
        cursor: pointer;
    }
<a href="#res-mobile" class="scroller" onclick="void(0)">
  <div id="mobile-hero-book" class="book-now-btn">Book a Car</div>
</a>
jquery ios css click
2个回答
0
投票

所以如果我理解你而不是使用href你可以尝试使用onclickJS中使用这样的函数:

function ANYTHING() {
   $("a[href^=#]").on('click touchstart', function(e) {
   e.preventDefault(); var dest = $(this).attr('href');  
   console.log(dest);
   $('html,body').animate({ scrollTop: $(dest).offset().top }, 'slow');
  });
}
.scroller {
            cursor: pointer;
        }
<a class="scroller" onclick="void(0)" onclick='ANYTHING('>
   <div id="mobile-hero-book" class="book-now-btn">Book a Car</div>
</a>

如果有帮助,请告诉我!如果需要,你仍然可以将href保留在html中!


0
投票

试试我为你遇到的问题编写的这个小jQuery插件:

$(document).ready(function() {
  $("#stupid-button").touch(function() {
    console.log("The stupid button was pressed!");
  });
});

$.fn.touch = function(callback) {
  var touch = false;
  $(this).on("click", function(e) {
    if (!touch) {
      console.log("Click detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    } else {
      touch = true;
    }
    touch = false;
  });
  $(this).on("touchstart", function(e) {
    $(this).blur();
    if (typeof e.touches != typeof undefined) {
      e.preventDefault();
      touch = true;
      console.log("Touch detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="stupid-button">Click Me</button>

编辑1:

删除

编辑2:

我误解了这个问题。上面的代码对iOS设备仍然有用,但是这是处理锚标记应该做的。摆脱#id href:

$(document).ready(function() {
  $(".anchor").touch(function() {
    var targ = $("#" + $(this).data('targ'));
    $('html,body').animate({
      scrollTop: targ.offset().top
    }, 'slow');
  });
});

$.fn.touch = function(callback) {
  var touch = false;
  $(this).on("click", function(e) {
    if (!touch) {
      console.log("Click detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    } else {
      touch = true;
    }
    touch = false;
  });
  $(this).on("touchstart", function(e) {
    $(this).blur();
    if (typeof e.touches != typeof undefined) {
      e.preventDefault();
      touch = true;
      console.log("Touch detected!");
      let callbackReal = callback.bind(this);
      callbackReal(this, e);
    }
  });
}
#target-ele {
  margin-top: 1200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="javascript:void(0);" class="anchor" id="link-1" data-targ="target-ele">Click Me</a>

<div id="target-ele">
  <p>This is the target container</p>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.