简体   繁体   English

切换而不是转到带有URL的页面

[英]Toggle instead of going to page with URL

I'm creating different style of navigation in mobile view. 我正在移动视图中创建不同的导航样式。 The three links that has + sign has their link on desktop view but I want in mobile when clicked, the sub menu will toggle. 带有+号的三个链接在桌面视图上都有其链接,但是我想在移动设备上单击时会切换子菜单。 How can I do that if the 3 links has href? 如果3个链接具有href,该怎么办? How to prevent the link going to the another page? 如何防止链接转到另一个页面?

在此处输入图片说明

This is my code 这是我的代码

$(window).resize(function() {
var width = $(document).width();
if (width >= 320) {
    $("ul#main-menu li").on("click", function() {
      $(this).next().find('ul.sub-menu').css("display", "block");
      $(this).next().find('ul.sub-menu').slideToggle(300);
      return false;
    });
}

else {
    $("ul#main-menu li").each(function() {
    $(this).hover(function() {
        var thiswidth = $(this).css('width');
        thiswidth = parseInt(thiswidth);
        thiswidth = thiswidth / 2;
        var nbsub = $(this).find('.sub-menu').children().length;
        submulti = 257;
        if($(this).hasClass('petite')){
            submulti = 206;
        }
        subwidth = (nbsub * submulti);
        subwidthhalf = subwidth / 2;
        if($(this).hasClass('premier')){
            subwidthhalf = subwidthhalf - 75 ;
        }
        if($(this).hasClass('milieu')){
            subwidthhalf = subwidthhalf + 156;
        }
        if($(this).hasClass('dernier')){
            subwidthhalf = subwidthhalf + 368 ;
        }
        subleft = (thiswidth - subwidthhalf);           
        $(this).find('.sub-menu').css('width',subwidth + 'px');
        $(this).find('.sub-menu').css('left',subleft + 'px');
        $(this).find('.sub-menu').css('display','block');
        }, function() {
        $(this).find('.sub-menu').css('width','auto');
        $(this).find('.sub-menu').css('left','-9999px');
        $(this).find('.sub-menu').css('display','none');
    });
});

}
});

When you add an event binder, the first parameter to the function you define will be the event itself. 添加事件绑定程序时,定义的函数的第一个参数将是事件本身。 Knowing that, there are various things you can call on an event. 知道这一点,您可以在事件上调用各种方法。 One thing is preventDefault which would prevent the default action. 一件事是preventDefault ,它将阻止默认操作。 You would modify your code to this (assuming the a or anchor tags with the href are inside the li tags): 您可以对此进行修改(假设带有hrefa或anchor标记位于li标记内):

if (width >= 320) {
    $("ul#main-menu li").on("click", function(evt) {
      evt.preventDefault();
      $(this).next().find('ul.sub-menu').css("display", "block");
      $(this).next().find('ul.sub-menu').slideToggle(300);
      return false;
    });
}

If you want it to happen even if the width is < 320, you'd need to add another event binding to do the same thing. 如果您希望即使宽度小于320也会发生这种情况,则需要添加另一个事件绑定来执行相同的操作。 Like so: 像这样:

if (width >= 320) {
    $("ul#main-menu li").on("click", function(evt) {
      evt.preventDefault();
      $(this).next().find('ul.sub-menu').css("display", "block");
      $(this).next().find('ul.sub-menu').slideToggle(300);
      return false;
    });
} else {
   $("ul#main-menu li").on("click", function(evt) {
      evt.preventDefault();
   });
   ... 
}

Note that it would be cleaner to target the anchor tags directly and prevent the click action from triggering the default behavior. 请注意,直接定位定位标记并防止click操作触发默认行为会更清洁。 Mostly because it would be more obvious what you're targeting and why for other programs who come along later. 主要是因为您将定位的目标以及为什么后来出现的其他程序会更加明显。

MDN: Event.preventDefault() MDN:Event.preventDefault()

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

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