Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 引导下拉菜单阻止切换到身体点击?_Jquery_Twitter Bootstrap_Dropdown - Fatal编程技术网

Jquery 引导下拉菜单阻止切换到身体点击?

Jquery 引导下拉菜单阻止切换到身体点击?,jquery,twitter-bootstrap,dropdown,Jquery,Twitter Bootstrap,Dropdown,我有这个菜单,我的问题是,我需要它的行为,使其仅在单击父菜单项时切换。现在你可以通过点击身体的某个地方来关闭下拉列表,这不是我想要的行为。只有在单击父菜单项时,下拉列表才应关闭。可能的怎么做 Html: 当用户单击文档时,您需要停止触发任何内容。在JS之后添加类似的内容: $(document).on('click', function(e) { $('.dropdown').find('.dropdown-menu').stop(); }); 单击文档而不是下拉元素时,可以停止事件传

我有这个菜单,我的问题是,我需要它的行为,使其仅在单击父菜单项时切换。现在你可以通过点击身体的某个地方来关闭下拉列表,这不是我想要的行为。只有在单击父菜单项时,下拉列表才应关闭。可能的怎么做

Html:


当用户单击文档时,您需要停止触发任何内容。在JS之后添加类似的内容:

$(document).on('click', function(e) {
    $('.dropdown').find('.dropdown-menu').stop();
});

单击文档而不是下拉元素时,可以停止事件传播:

$(文档)。在('click',':not(.dropdown')上,函数(e){
if($(this).closest('.dropdown')。长度==0){
//
//如果在文档中而不是在下拉列表中。。。。
//
e、 停止传播();
}
});
$('.dropdown').on('show.bs.dropdown',函数(){
$(this).find('.dropdown menu').first().stop(true,true.slideDown();
$(this).find('a.dropdown-toggle').removeAttr('data-toggle');
});
$('.dropdown').on('hide.bs.dropdown',函数(){
$(this).find('.dropdown menu').first().stop(true,true.slideUp();
});

    Ekonomiförvaltning
    Lönehantering
  • Programvaror

它可以工作,但并不完美。单击文档后需要单击两次才能关闭。见更新的小提琴。你说得对,抱歉。请尝试以下操作:
$(document).on('click',function(e){if(!$(e).hasClass('dropdown-toggle')){$('dropdown').find('dropdown menu').stop();})这只允许您在单击范围时关闭下拉列表。下拉列表开关我没有注意到任何差异。在单击文档之后,关闭仍然需要在跨度上单击两次,这是我不想要的。更新的fiddle:[link]()它干扰了我的覆盖,但将$(this).nestest()更改为$(e.target).nestest()似乎起了作用。
$('.dropdown').on('show.bs.dropdown', function () {
      $(this).find('.dropdown-menu').first().stop(true, true).slideDown();
      $(this).find('a.dropdown-toggle').removeAttr('data-toggle');
    });
    $('.dropdown').on('hide.bs.dropdown', function (){
      $(this).find('.dropdown-menu').first().stop(true, true).slideUp();
    });
$(document).on('click', function(e) {
    $('.dropdown').find('.dropdown-menu').stop();
});