美文网首页
jQuery的click事件点击跳转失败-解决方案

jQuery的click事件点击跳转失败-解决方案

作者: BlankChan | 来源:发表于2017-06-28 14:29 被阅读0次

click点击失效

// HTML代码:
<div class="report-item-block review-report" data-id="7179" data-tid="33761934">
    <div class="item-block-profile">
      <div class="profile-img">
        
          ![](/avatar.png)
        
      </div>
      <div class="profile-name">大文文啊</div>
    </div>
    <div class="item-block-content">
      <p>昨天下午快递小哥送来的,申通快递,挺高兴的,<昨天下午快递小哥送来的,申通快递,挺高兴的,昨天下午快递小哥送来的,申通快递,挺高兴的,昨天下午快递小哥送来的,申通快递,挺高兴的,昨天下午快递小哥送来的,申通快递,挺高兴的,昨天下午快递小哥送来的,申通快递,挺高兴的。/p>
    </div>
    <div class="item-block-time">2016-12-06</div>
  </div>

// js代码:
function onReviewReport(e) {
  // e.preventDefault();
  var $target = $(e.currentTarget);
  var tid = $target.data('tid');
  var id = $target.data('id');

  if (tid && tid != '0') {
    jsbridge.openTopic(tid);
  } else {
    Turbolinks.visit('/lists-review-report?id=' + id + '&' + window.moduleMeta.qs);
  }
}

$('body').on('click', '.review-report', onReviewReport);

解决方案(以下验证都是可行的):

1.直接把click事件绑定在触发对象上

$('.review-report').on('click', onReviewReport);

2.改click事件为touch事件

touch事件过程:
    touchstart -> touchend:触发某些事件函数
    touchstart -> touchmove -> touchend:不触发某些事件函数

var ismove = false;
$('body')
    .on('touchmove', '.review-report', () => {
      ismove = true;
    })
    .on('touchstart', '.review-report', () => {
      ismove = false;
    })
    .on('touchend', '.review-report', (e) => {
      if (!ismove) {
        onReviewReport(e);
      }
    });

3.改目标对象为cursor: pointer;

因为click的事件是绑定在div上,在IOS上会有失效的情况,但是在Android和各种模拟器中一切正常。

在网上找到一个说法:

“后来经过查找资料才知道,苹果有这么个设置: 
对于点击的对象,拥有cursor:pointer这个样式的设置,也就是说,鼠标放上去,能够出现“手”型的图标才被认作可以使用点击事件,于是果断增加了样式cursor: pointer;”

参考链接:http://blog.csdn.net/yuexiage1/article/details/51612496

相关文章

网友评论

      本文标题:jQuery的click事件点击跳转失败-解决方案

      本文链接:https://www.haomeiwen.com/subject/sleucxtx.html