【问题标题】:Popover not hiding when leaving the textbox in Opera 12在 Opera 12 中离开文本框时弹出框不隐藏
【发布时间】:2014-09-02 07:59:49
【问题描述】:

我正在使用以下脚本在 Bootstrap 3 中使用 HTML 支持显示焦点弹出框:

$(document).ready(function () {
    $(".focus-popover").each(function (index) {
        var showPopover = function () {
            $(this).popover('show');
        };
        var hidePopover = function () {
            $(this).popover('hide');
        };
        $(this).popover({
            html: true,
            placement: $(this).attr('data-placement'),
            trigger: 'manual'
        })
        .focus(showPopover)
        .blur(hidePopover);
    })
});

但是这个输入...

<input type="date" ID="test2" class="form-control focus-popover" data-original-title="This is my title" data-placement="top" data-container="body" data-content="Click away and see how this will be dismissed.&lt;br /&gt;However, on Opera 12 it will remain."></input>

... 在 Opera 12 中存在某种错误。由于输入类型是“日期”而不是“文本”,因此在离开文本框时不会隐藏弹出框。

请查看 Opera 12 以及任何其他浏览器中的 this Example

我该怎么做才能让它正常工作?

【问题讨论】:

  • 输入标签不能有内容,所以你不应该有&lt;/input&gt;
  • Bootstrap 没有正式支持 Opera 12,所以,祝你好运。

标签: javascript twitter-bootstrap twitter-bootstrap-3 opera popover


【解决方案1】:

显然,日期选择器会阻止浏览器在通过单击离开失去焦点后触发模糊事件。

只有在您保持制表键失去焦点或通过选择日期更改值时,它才会触发模糊事件。

因此,基本上可以通过使用另一个元素的单击/焦点来模拟模糊事件。

解决方法

$(document).ready(function () {
    $(".focus-popover").each(function (index) {
        var showPopover = function () {
            $(this).popover('show');
        };
        var hidePopover = function () {
            $(this).popover('hide');
        };
        $(this).popover({
            html: true,
            placement: $(this).attr('data-placement'),
            trigger: 'manual'
        })
        .focus(showPopover)
        .blur(hidePopover);
    })
    // The nasty Opera < 12 "workaround"
    if ( window.opera && +window.opera.version() <= 13 ) {
        var $buggyInput = $("#test2"), // Caching is important!
            buggyInputFocus = false,
            buggyFocus = function(event) {
                event.stopPropagation();
                if(!buggyInputFocus) {
                    $(this).popover('show');
                    buggyInputFocus = true;
                }
            },
            buggyBlur = function(event) {
                $(this).popover('hide');
                buggyInputFocus = false;
            }
        ;
        // The focus also stop triggering if it was blurred then clicked back, so we added a click. It doesn't run buggyFocus twice since it only execute itself if it hasn't been focused first
        $buggyInput.on({
            "focus": buggyFocus,
            "click": buggyFocus,
            "blur":buggyBlur,
            "change":buggyBlur // On change is also important, you don't want to leave it open when it changes
        })
        // Since it doesn't the blur event, we fake it by capturing focus or click on the html tag
        $("html").on({
            click: function() {
                if ( buggyInputFocus ) $buggyInput.trigger("blur");
            },
            focus: function() {
                if ( buggyInputFocus ) $buggyInput.trigger("blur");
            }
        })
    }
});

小提琴:http://jsfiddle.net/5wsq38u3/4/

编辑:超过 1 个日期输入

$(document).ready(function () {
    $(".focus-popover").each(function (index) {
        var showPopover = function () {
            $(this).popover('show');
        };
        var hidePopover = function () {
            $(this).popover('hide');
        };
        $(this).popover({
            html: true,
            placement: $(this).attr('data-placement'),
            trigger: 'manual'
        })
        .focus(showPopover)
        .blur(hidePopover);
    })
    // The nasty Opera < 12 "workaround"
    if (window.opera && +window.opera.version() < 13) {
        var $buggyInputs = $(".operaFix"), // Caching is important!
            buggyInputFocus = false,
            buggyInput = {}, // We store an instance of the focused element
            buggyFocus = function(event) {
                event.stopPropagation();
                if(!buggyInputFocus) {
                    $(buggyInput).popover('hide');
                    $(this).popover('show');
                    buggyInputFocus = true;
                    buggyInput = $(this);
                }
                else if ($(buggyInput).attr("id") !== $(this).attr("id")){
                   $(buggyInput).trigger("blur")
                }
            },
            buggyBlur = function(event) {
                $(this).popover('hide');
                buggyInputFocus = false;
                buggyInput = {}
            }
        ;
        // The focus also stop triggering if it was blurred then clicked back, so we added a click. It doesn't run buggyFocus twice since it only execute itself if it hasn't been focused first
        $buggyInputs.on({
            "focus": buggyFocus,
            "click": buggyFocus,
            "blur": buggyBlur,
            "change": buggyBlur // On change is also important, you don't want to leave it open when it changes
        })
        // Since it doesn't the blur event, we fake it by capturing focus or click on the html tag
        $("html").on({
            click: function() {
                if (buggyInputFocus) $(buggyInput).trigger("blur");
            },
            focus: function() {
                if (buggyInputFocus) $(buggyInput).trigger("blur");
            }
        })
    }
});

JSBin:http://jsbin.com/xucagomutera/1/edit

【讨论】:

  • 感谢您的回复。只要我的网站上只有一个日期文本框,这就行得通。你也可以让它与多个日期控件一起工作吗?请参阅 this 并在两个日期字段之间跳过以了解我的意思。
  • 在那里,我用多个输入元素的替代方法编辑了响应
  • 在这些日期输入之间切换时它会闪烁一点,但它工作得很好,对于 Opera 12 用户来说应该足够了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2015-12-19
  • 2013-07-24
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多