简体   繁体   English

Bootstrap v2 下拉导航不适用于移动浏览器

[英]Bootstrap v2 Dropdown Navigation not working on Mobile Browsers

I am having an issue with the Bootstrap menu dropdowns on mobile devices (Bootstrap 2).我在移动设备(Bootstrap 2)上的 Bootstrap 菜单下拉菜单有问题。 A similar question was asked here with dropdown buttons, however the answer for that was an inherent bug within bootstrap which was solved in an update. 这里使用下拉按钮询问类似的问题,但答案是引导程序中的一个固有错误,该错误已在更新中解决。 I appear to be having the same issue so perhaps it's down to my markup?我似乎遇到了同样的问题,所以也许这取决于我的标记?

I have a collapsable navbar with dropdowns and everything works perfectly on desktop browsers.我有一个带有下拉菜单的可折叠导航栏,一切都在桌面浏览器上完美运行。 However on a mobile, the dropdowns will open up when you click on the dropdown but clicking any dropdown links will just fold the dropdown back up again — the links cannot be reached.但是在移动设备上,当您单击下拉菜单时会打开下拉菜单,但单击任何下拉链接只会将下拉菜单再次折叠起来——无法访问链接。 I have tried various bootstrap versions and cannot correct this so I can only imagine it is my markup.我尝试了各种引导程序版本,但无法纠正这个问题,所以我只能想象这是我的标记。 Here it is:这里是:

 <header class="navbar">
    <div class="navbar-inner">
        <div class="container">
            <a href="#"><h1>Branding</h1></a>
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> Menu </button>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    <li><a href="#">Menu Item 1</a></li>
                    <li class="dropdown">                    
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu Item 2 (Dropdown)<b class="caret"></b></a>      
                        <ul class="dropdown-menu">
                            <li><a href="#">Dropdown Item 1</a></li>
                            <li><a href="#">Dropdown Item 2</a></li>
                            <li><a href="#">Dropdown Item 3</a></li>  
                            <li><a href="#">Dropdown Item 4</a></li>                           
                        </ul>
                    </li>
                    <li><a href="#">Menu Item 3</a></li>
                    <li><a href="#">Menu Item 4</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div>
</header>

Here's an example replicating the code (sorry, can't send the site): http://jsfiddle.net/yDjw8/1/这是复制代码的示例(抱歉,无法发送站点): http : //jsfiddle.net/yDjw8/1/

(Problem can only be seen/replicated on mobile — I'm using iOS) (问题只能在移动设备上看到/复制 - 我使用的是 iOS)

Any help would be much appreciated.任何帮助将非常感激。

In your minified bootstrap.min.js file, find the substring在缩小的 bootstrap.min.js 文件中,找到子字符串

"ontouchstart"

and replace it with并将其替换为

"disable-ontouchstart"

Check out this similiar SO question: Bootstrap Collapsed Menu Links Not Working on Mobile Devices看看这个类似的问题: Bootstrap Collapsed Menu Links Not Working on Mobile Devices

Found this fix for version 2.3.2:为 2.3.2 版找到了此修复程序:

<script>
jQuery(document).ready(function($)  {
$("li.dropdown a").click(function(e){
    $(this).next('ul.dropdown-menu').css("display", "block");
    e.stopPropagation();
  });
}); </script>     

Worked on two separate nav systems I built.在我构建的两个单独的导航系统上工作。

I am using BootStrap 3.1.1.我正在使用 BootStrap 3.1.1。 I have tried many answers, dropdown menu works fine on Android devices but still doesn't works on iOS device.我尝试了很多答案,下拉菜单在 Android 设备上运行良好,但在 iOS 设备上仍然不起作用。 Finally I find root cause of the problem.最后我找到了问题的根本原因。

safari on iPhone only support click event for <a> and <input> element . iPhone 上的 safari 仅支持<a><input>元素的点击事件 See this passage Click event delegation on the iPhone .参见这段iPhone 上的 Click 事件委托

So we need to add custom click delegation.所以我们需要添加自定义点击委托。 Following code will solve the problem.以下代码将解决问题。

$('[data-toggle=dropdown]').each(function() {
 this.addEventListener('click', function() {}, false);
});

I solved this same problem doing this:我这样做解决了同样的问题:

1.Open your js/bootstrap-dropdown.js or the minified version of it. 1.打开你的js/bootstrap-dropdown.js或者它的缩小版本。

2.Find for the lines or places for: touchstart.dropdown.data-api 2.查找线路或位置: touchstart.dropdown.data-api

3.There should be only 4 occurs of it. 3.应该只有4次出现。 Something like this:像这样的东西:

.on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus)
.on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
.on('click.dropdown.data-api touchstart.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
.on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)

You should insert this new line between the 2nd and 3rd occurences:您应该在第 2 次和第 3 次出现之间插入此新行:

.on('touchstart.dropdown.data-api', '.dropdown-menu', function (e) { e.stopPropagation() })

4.Your news piece of code must be something like this: 4.你的新闻代码必须是这样的:

.on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus)
.on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
.on('touchstart.dropdown.data-api', '.dropdown-menu', function (e) { e.stopPropagation() })
.on('click.dropdown.data-api touchstart.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
.on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)

Be careful on minified versions of Bootstrap.js... so you must concatenate the new line at the exact position on it.小心 Bootstrap.js 的缩小版本...所以你必须在它的确切位置连接新行。

Good luck!祝你好运! :) :)

I recommending downloading the latest Bootstrap files and replacing the bootstrap.js and bootstrap.min.js on your server with the new versions.我建议下载最新的 Bootstrap 文件并用新版本替换服务器上的 bootstrap.js 和 bootstrap.min.js。

This fixed the issue for me.这为我解决了这个问题。

I found solution of this from bootstrap git-hub forum.我从 bootstrap git-hub 论坛找到了这个解决方案。 You need to include just one line of code in script on document ready as,您只需要在准备好文档的脚本中包含一行代码,

$('body').on('touchstart.dropdown', '.dropdown-menu', function (e) { e.stopPropagation(); }); $('body').on('touchstart.dropdown', '.dropdown-menu', function (e) { e.stopPropagation(); });

Its work for me!!!它为我工作!!!

This fix is for Bootstrap 2.3.2, and its based on the answer by @asbanks ( https://stackoverflow.com/a/18107103/437019 ).此修复适用于 Bootstrap 2.3.2,它基于 @asbanks ( https://stackoverflow.com/a/18107103/437019 ) 的回答。

On top of asbanks's answer, this script also propagates JS calls from links inside the dropdowns, and an opened dropdown closes other open ones.除了 asbanks 的回答之外,该脚本还会从下拉列表中的链接传播 JS 调用,并且打开的下拉列表会关闭其他打开的下拉列表。

$(function() {
  return $("a.dropdown-toggle:not(.multiselect)").click(function(e) {
    $("ul.dropdown-menu:not(.multiselect-container)").css("display", "none");
    $(this).next("ul.dropdown-menu").css("display", "block");
    return e.stopPropagation();
  });
});

$(document).on('click click.dropdown.data-api', function(e) {
  if (!$(e.target).closest('.dropdown, .multiselect-container').length) {
    return $("ul.dropdown-menu:not(.multiselect-container)").css("display", "none");
  }
});

This seems to work without any issues.这似乎没有任何问题。 The class "open" already exists with bootstrap and should be working, but for some reason it isn't.类“open”已经存在于引导程序中,应该可以工作,但由于某种原因,它不是。 This code forces it to run accordingly.此代码强制它相应地运行。 :) :)

    jQuery(document).ready(function($) {
    $("li.dropdown").click(function(e){
        $(this).toggleClass("open");
        });
    });

Looks like it is all working to me, maybe try replacing your bootstrap files with a fresh copy incase you accidentally messed anything up in there.看起来这一切对我有用,也许可以尝试用新副本替换引导文件,以防万一你不小心弄乱了那里的任何东西。 Or if that does not work, make sure you are importing everything.或者,如果这不起作用,请确保您正在导入所有内容。 Are you sure you are importing all the CSS and the JS files?您确定要导入所有 CSS 和 JS 文件吗?

When you click on the dropdown, it adds the open class to your <li class="dropdown"> giving you: <li class="dropdown open"> .当您单击下拉菜单时,它会将open类添加到您的<li class="dropdown">为您提供: <li class="dropdown open"> When you click a link on the dropdown menu or the 'Menu item 2 (dropdown)', the open class is removed causing the dropmenu to fold up again.当您单击下拉菜单或“菜单项 2(下拉菜单)”上的链接时, open类将被删除,从而导致下拉菜单再次折叠。

The below fiddle shows how to stop the click event from propagating, but may cause you issues else where.下面的小提琴显示了如何阻止点击事件传播,但可能会导致您在其他地方出现问题。 Also, I only prevented the propagation on item #1 in the dropdown.另外,我只阻止了下拉列表中第 1 项的传播。 You can use jQuery to have this happen on all items of the dropdown.您可以使用 jQuery 在下拉列表的所有项目上发生这种情况。

JS Fiddle: http://jsfiddle.net/yDjw8/2/ JS小提琴: http : //jsfiddle.net/yDjw8/2/

I am using bootstrap-3.3.1 and I am experiencing the same problem in a Android phonegap app.我正在使用 bootstrap-3.3.1,我在 Android phonegap 应用程序中遇到了同样的问题。

I found a funny workaround after several trials and errors:经过多次试验和错误,我找到了一个有趣的解决方法:

// clueless hack here!!!! otherwise dropdown menu doesn't work
$('.dropdown-toggle').dropdown('toggle');
$('.dropdown-toggle').dropdown('toggle');

role="button"添加到<a href="#" class="dropdown-toggle" data-toggle="dropdown">Menu Item 2 (Dropdown)<b class="caret"></b></a>为我工作

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Menu Item 2 (Dropdown)<b class="caret"></b></a>

jquery-1.11.2, bootstrap-3.3.2: jquery-1.11.2,bootstrap-3.3.2:

$('#yourId').on('hidden.bs.dropdown', function (){
    $(this).click(function (event) {
       $('.dropdown-toggle').dropdown('toggle'); 
    });
});

I reckon if you do the following, the links will work properly我认为如果您执行以下操作,链接将正常工作

$(document).ready(function(){
            $('.youClass').click(function(){
                var menuItem = '<a class=" " href=" ">Log Out</a>';

                $('.anotherClass').append('<li class=".youClass">'+ menuItem +'</li>');

            });
        });

I have to worked with Bootatrap v2.2.1.我必须使用 Bootatrap v2.2.1。 Dropdown was worked on desktop but immediately closing after opening on mobile. Dropdown 在桌面上工作,但在移动设备上打开后立即关闭。

And I think the problem is that old version doesn't expect to be work with touchable devices.而且我认为问题在于旧版本不希望与可触摸设备一起使用。

So my fix based on preventing some touch events:所以我的修复基于防止一些触摸事件:

$('a.dropdown-toggle').on('touchstart', function (e) {
  e.preventDefault();
});

In rare cases, you may need to add:在极少数情况下,您可能需要添加:

  $('a.dropdown-toggle').on('touchmove', function (e) {
      e.preventDefault();
  });

  $('a.dropdown-toggle').on('touchcancel', function (e) {
      e.preventDefault();
  });

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

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