【问题标题】:How do I determine the source of a Twitter Bootstrap component event?如何确定 Twitter Bootstrap 组件事件的来源?
【发布时间】:2013-07-31 01:56:10
【问题描述】:

如果我在元素上使用工具提示和弹出 Twitter Bootstrap Javascript 组件,我如何确定哪个组件触发了“显示”事件?

例如:

$("#button").tooltip();
$("#button").popover();

$("#button").on("shown", function(event) {
    // I need to do some (flash) stuff that's specific to the popover content,
    // the shown event is fired for both the tooltip and popover but doesn't 
    // appear to include any useful information about the context of the event.

    // How do I determine if the popover component fired the event? The event.target
    // is useless because it points to #button.
    // The best I've come up with is to check if the element I'm interested in 
    // is visible but I'm assuming this will have issues if the popover component
    // is animated...
    if ($(".popover .thingy").is(':visible')) {
    }
});

【问题讨论】:

    标签: javascript twitter-bootstrap tooltip jquery popover


    【解决方案1】:

    由于使用了jQuery,我相信event代表的当前元素就是:

    this
    

    或者,

    $(this) // to do jQuery operations on it
    

    如果要检查是否可见:

    if ($(this).is(":visible")) { ... }
    

    【讨论】:

    • 我试试看。根据以前的经验,我假设this 将引用“#button”...
    • 是的,它会......我认为这就是你要找的东西,如果不是,那么“这个”将不起作用。您正在寻找随后显示的 POPOVER 元素,而不是触发弹出框的元素,对吗?
    • 是的,我需要知道弹出框何时显示(不是工具提示)。看起来 Bootstrap 使用 '#button' 作为事件源(event.target == '#button'),所以this 指的是按钮。我希望事件的属性可以指示正在显示的内容(例如 event.popoverCreatedThisEvent)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2015-07-17
    • 2012-03-21
    相关资源
    最近更新 更多