在Bootstrap 3中第二次单击时导航到下拉父级链接

我正在尝试实现允许用户在第二次点击时导航到移动设备上下拉列表项锚点的href的function。 但是,如果列表项目当前具有“打开”类,指定当前已单击链接一次,我只希望发生这种情况。 我尝试过以下方法:

$('.dropdown').on('hide.bs.dropdown', function () { if ($(this).hasClass('open')) { window.location = $(this).find('a.dropdown-toggle.visible-sm').href(); } }); 

和:

 $('.dropdown.open').on('hide.bs.dropdown', function () { window.location = $(this).find('a.dropdown-toggle.visible-sm').href(); }); 

但两者都会导致用户导航到任何其他下拉链接的href,而不会展开下拉列表。 在进一步研究之后,我发现一些文章说,当动态添加类时,您需要使用事件委托来引用动态添加的类。 我试过这个:

 $(document).on('click', 'li.open', function(){ window.location = $(this).find('a.dropdown-toggle.visible-sm').href(); }); 

但它与我之前尝试的相同。

Tony的解决方案对我来说不起作用,因为没有一些额外的JavaScript函数就无法关闭下拉列表。

如果下拉列表已经打开,我打开页面。

 $('.dropdown a.dropdown-toggle').on('click', function() { if($(this).parent().hasClass('open')) location.assign($(this).attr('href')); }); 

我能够解决这个问题以防万一有人需要这样做:

 $('.dropdown').on('show.bs.dropdown', function () { $(this).siblings('.open').removeClass('open').find('a.dropdown-toggle').attr('data-toggle', 'dropdown'); $(this).find('a.dropdown-toggle').removeAttr('data-toggle'); }); 

根据Pendrokar的回答,我使用它来覆盖所有下拉菜单,因为’open’并不总是被指定为类,并允许链接指定目标。

 $('a[data-toggle="dropdown"]:not([href=""])').off('click.namespace').on('click.namespace', function(e) { if ($(this).parent().is('.show, .open')) window.open($(this).attr( 'href'), $(this).attr('target') || '_self'); });