简体   繁体   English

在移动设备的狩猎中, <a href=“#” onclick=“return false”>不起作用,为什么?</a>

[英]In mobile devices' safari, <a href=“#” onclick=“return false”> doesn't work, why?

In moblie devices' safari, like iphone or ipad, <a href="#" onclick="return false"> doesn't prevent default behaviour, the page was still redirected to '#', why...? 在移动设备的游戏中,如iphone或ipad, <a href="#" onclick="return false">不会阻止默认行为,页面仍然被重定向到'#',为什么......? Like these html code: 像这些HTML代码:

<div style="height:1000px; width:100px"></div>
<br />    
<a href="#" onclick="return false">click me</a>

When click in moblie devices' safari, it goes back to the top of the page... 当点击moblie设备的safari时,它会回到页面顶部...

I had the same problem. 我有同样的问题。 It turns out that it is a bug in the iOS 5 version of Safari. 事实证明,它是iOS 5版Safari中的一个错误。 Doesn't happen in newer or older versions, or any other browser or platform. 在较新版本或较旧版本或任何其他浏览器或平台中不会发生。

I resolved it by adding preventDefault to the onclick event handler, in addition to the existing return false , like so: 我解决了它,除了现有的return false之外,还将preventDefault添加到onclick事件处理程序中,如下所示:

<a href="#" onclick="event.preventDefault(); return false;">click me</a>

Not ideal, but it does solve the problem. 不理想,但确实解决了这个问题。

I just kinda had the same problem with my links not working right on my friend's iPhone. 我只是有点同样的问题,我的链接不能正常在我朋友的iPhone上工作。 I deleted the href="#" from mine and the buttons work perfectly fine, except I have my buttons set up a little differently than you with the JavaScript. 我删除了我的href="#"并且按钮工作得非常好,除了我的按钮设置与JavaScript有点不同。

The way I have it set up is like this 我设置它的方式是这样的

$(document).ready(function () {
    var buttons=$('#button01,#button02,#button03,#button04,#button05');

buttons.click(function(){
        $(this).removeClass('off-class').addClass('on-class');
        buttons.not(this).removeClass('on-class').addClass('off-class');


})

Then I trigger the frames I want to pop up with 然后我触发我要弹出的帧

if ($('#button01').hasClass('on-class')){
                $('#frame01').removeClass('hidden');
$('#frame02,#frame03,#frame04,#frame05').addClass('hidden');
                }else{
                    $('#frame01').addClass('hidden');

I don't know how you have yours set up, but removing the href="#" with this set up worked on mine. 我不知道你是如何设置的,但是使用这个设置删除了href="#"

Seems to be tricky. 似乎很棘手。 I tested with this code: 我测试了这段代码:

<body>
Hallo
<br>
<div style="height:2000px; width:100px"></div>
<br />    
<a href="#" onclick="alert('click');return false;" 
            onmousedown="alert('down');return false;"
            onmouseup="alert('up');return false;"
            onmouseover="alert('over');return false;"
            onmouseout="alert('out');return false;">

click me</a>
</body>

What happens is, that when I refresh the page on iPhone and tap for the first time I get: 会发生什么,当我在iPhone上刷新页面并第一次点击时,我得到:

  1. over 过度
  2. down
  3. up 向上
  4. click 点击
  5. scroll up 向上滚动

Every next tap I get: 每下一次点击我得到:

  1. down
  2. up 向上
  3. click 点击
  4. No scroll up 没有向上滚动

Funny enough, this works only, when the code contains the alerts. 有趣的是,这只适用于代码包含警报的情况。 If no alerts inside code, scrolling up happens every time I tap.... 如果代码中没有警报,每次点击时都会向上滚动....

For sure there's some magic with hrefs in mobile safari, which you can see, when you hold the link (without lifting the finger): An action sheet appears ("copy", "open", "open in ..." etc.) before one of the events is fired. 当你拿着链接(没有抬起手指)时,你可以看到移动游猎中的hrefs有一些神奇之处:出现一个动作表(“复制”,“打开”,“打开......”等。 ) 其中一个事件被解雇之前。

I actually tried with 我实际上尝试过

<a href="javascript:void(0)" onclick="return false">click me</a>

and it works well 而且运作良好

Why not? 为什么不? I think, you must call a function and not only return false... 我想,你必须调用一个函数,而不仅仅是返回false ...

<a href="#" onclick="javascript:myFunction()" id="test_a">click me</a>

function myFunction() {
  var elem = document.getElementById('test_a');
  if (elem.addEventListener) {
      elem.addEventListener('click', function() {
          alert('clicked');
      }, false);
  } else {
      elem.attachEvent('onclick', function() {
          alert('clicked');
      });
  }

  return false;
}

Firstly, try this: 首先,试试这个:

<a href="#" id='test'>click me</a>

in js: 在js中:

$('a#test').click(function(event){
    event.preventDefault();
});

Secondly(if the first variant will not work): 其次(如果第一个变体不起作用):

<a href="#" id='test' onclick='return DummyFunction();'>click me</a>

in js: 在js中:

function DummyFunction(){
    return false;
}

As far as i remember, one of these variant solved the same problem, when i had it. 据我所知,当我拥有它时,其中一个变体解决了同样的问题。

The first variant is much better, becouse you don't mix html and javascript 第一个变体要好得多,因为你不混用html和javascript

Basically, I went with a similar solution, but backed up a bit more with some conditional checking ... this is jQuery based and used for login in Sitefinity. 基本上,我使用了类似的解决方案,但通过一些条件检查支持了更多...这是基于jQuery并用于在Sitefinity中登录。

  if ($.browser.safari) {
    $('.sfSubmitBtn').attr('onclick', 'event.preventDefault(); return false;');
  }

I did find that using the event.preventDefault() caused a similar issues to begin occurring in some versions of IE. 我确实发现使用event.preventDefault() 导致在某些版本的IE中开始出现类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM