简体   繁体   English

Twitter Bootstrap .on('show',function(){}); 不适用于popover

[英]Twitter Bootstrap .on('show',function(){}); not working for popover

I am trying to hide all other popovers when a new popover is selected by doing the following: 我通过执行以下操作,在尝试选择新的弹出框时尝试隐藏所有其他弹出窗口:

My HTML 我的HTML

a.btn#requests(rel='popover',data-placement='bottom',data-original-title='<b>Requests</b>',data-content='My content goes here')

My Javascript 我的Javascript

  $(function (){
    console.log('start');
    $('#requests').popover();
    $('#messages').popover();

  });

  //This doesn't work for some reason?
  $('#requests').on('show', function (e) {
    console.log('requests');
    $('#messages').popover('hide');
  });

  $('#messages').on('show', function () {
    console.log('messages');
    $('#requests').popover('hide');
  });

However, my console.log('requests') and console.log('messages'); 但是,我的console.log('requests')和console.log('messages'); is never getting shown even though the requests and messages popovers are showing, what am I doing wrong? 即使请求和消息弹出窗口正在显示,我仍然没有显示,我做错了什么?

Bootstrap now supports popover events - see the Events section in the official popover docs (and here's the doc changelog ). Bootstrap现在支持popover事件 - 请参阅官方popover文档中的Events部分( 这里是doc changelog )。

Example: 例:

$('#requests')
.popover()
.on('show.bs.popover', function() { console.log('o hai'); })
.on('hidden.bs.popover', function() { console.log('ciao'); });

The popover plugin doesn't trigger any event. popover插件不会触发任何事件。 Neither does the tooltip plugin (since popover extends tooltip). 工具提示插件也没有(因为popover扩展了工具提示)。 Check this issue (github) for alternatives. 检查此问题(github)以寻找替代方案。

You can use different JS events depending on your trigger . 您可以根据trigger使用不同的JS事件。 For your example : Demo (jsfiddle) 对于你的例子: 演示(jsfiddle)

$(function (){
    console.log('start');
    $('#requests').popover();
    $('#messages').popover();

    $('#requests').on('click', function (e) {
        console.log('requests');
        $('#messages').popover('hide');
    });

    $('#messages').on('click', function () {
        console.log('messages');
        $('#requests').popover('hide');
    });

});

Why 'click' ? 为什么'click' Because the default popover trigger for version 2.1.1 is click . 因为版本2.1.1的默认弹出触发器是click See the popover doc (github) 查看popover doc(github)

You can use the following events : 您可以使用以下事件:

  • trigger: 'click' : on click trigger: 'click'click
  • trigger: 'hover' : display on mouseenter and hide on mouseleave trigger: 'hover' :在mouseenter上显示并隐藏在mouseleave
  • trigger: 'focus' : display on focus and hide on blur trigger: 'focus' :显示focus并隐藏blur
  • trigger: 'manual' : use your own code to display and hide anyway trigger: 'manual' :使用您自己的代码来显示和隐藏

you can easily do it by using class and haveing less and more organazied codes: 您可以通过使用类并使用越来越多的组织代码轻松地完成此操作:

$(document).ready(function(){

    $('.btn').popover();

    $('.btn').on('click', function (e) {
        $('.btn').not(this).popover('hide');
    });
});

check my demo here 在这里查看我的演示

and if you want to have form control inside customize this code: 如果你想在里面有表单控件自定义这段代码:

var mycontent = '<div class="btn-group"> <button class="btn">Left</button> <button class="btn">Middle</button> <button class="btn">Right</button> </div>'

$('.btn').popover({
        content:mycontent,
        trigger: 'manual'
}).click(function(e) {
    $(this).popover('toggle');
    e.stopPropagation();
});


   $('.btn').on('click', function (e) {
       $('.btn').not(this).popover('hide');
    });
});

check the demo here 这里查看演示

According to https://getbootstrap.com/docs/3.3/javascript/#popovers-events the name of the event is 根据https://getbootstrap.com/docs/3.3/javascript/#popovers-events ,事件的名称是

show.bs.popover show.bs.popover

I tried it, and it works fine 我试过了,它工作正常

Try this: 试试这个:

            $(document).on('click', function(e) {
                if (!$(e.target).is('[data-original-title]')) {
                    $('[data-original-title]').popover('hide'); 
                }
            });

            $(document).on('show.bs.popover', function(e) { 
                $('[data-original-title]').popover('hide'); 
            });

This sets a event handler on the whole document and if it's not a popover element it will hide all popovers. 这会在整个文档上设置一个事件处理程序,如果它不是一个popover元素,它将隐藏所有的popovers。

Also, on the event show.bs.popover (triggered prior to opening a popover) it will hide all others. 此外,在事件show.bs.popover(在打开弹出窗口之前触发)它将隐藏所有其他。

它是.on('shown')而不是.on('show')

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

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