解决MUI阻止a标签默认跳转事件—方法总结

  用过mui的小伙伴们必定不会陌生,有时候真的很烦mui自己会阻止a标签默认跳转。通常只要用了mui的ui组件,好比头部,底部或者弹框,你就不能在用a标签进行跳转了。html

  注:项目中引用了mui后,可能也会遇到,html代码中未引用mui的组件代码段,但依然会致使a标签默认跳转失效的问题(mui通常用于移动端)node

 

  在实际项目使用中,我总结了如下几种方法:ui

  ①:直接使用js或jq获取要点击的元素,并绑定相应的点击事件,而后在用window.location.href进行跳转,以下代码:this

$(function(){
    $("#index").on('tap', function() {
        window.location.href = '../index/index.html';
    });
    $("#classify").on('tap', function() {
        window.location.href = '../product/classify.html';
    });
    $("#vip").on('tap', function() {
        window.location.href = '../vip/vipCenter.html';
    });
    $("#shoppingCart").on('tap', function() {
        window.location.href = '../shopcart/shoppingcar.html';
    });
    $("#personal").on('tap', function() {
        window.location.href = '../personalCenter/index.html';
    });
});

 

  ②:直接注释mui中,阻止a标签默认跳转的源码部分 (不到万不得已通常不推荐直接修改或者注释源码url

 

  ③:当你想让某个页面的a标签跳转不受mui影响,但又不想使用上面2种方法时,能够在当前页面添加以下代码,亲测好用spa

mui(document).on('tap', 'a', function() {
    var a = document.createElement('a');
    a = this.cloneNode(true);
    a.click();
})
  cloneNode(true)属性介绍:  http://www.w3school.com.cn/jsref/met_node_clonenode.asp

  ④:其实mui官方也提供了相应的解决方法,官方连接 http://dev.dcloud.net.cn/mui/window/#openwindow代码以下: .net

//tap为mui封装的单击事件,解决移动端click事件点击延迟300毫秒的问题
document.getElementById('info').addEventListener('tap', function() {
  //打开关于页面
  mui.openWindow({
    url: 'examples/info.html', 
    id:'info'
  });
});

 

  小伙伴们能够根据状况选择使用哪种方法解决code

相关文章
相关标签/搜索