【问题标题】:'show' event doesn't fire in IE9 using Twitter Bootstrap Modal in Rails在 Rails 中使用 Twitter Bootstrap 模式在 IE9 中不会触发“显示”事件
【发布时间】:2012-08-01 20:32:14
【问题描述】:

我有一个涉及几个不同框架/库的问题,尽管主要是 Twitter Bootstrap(js) 和 Rails。

我的应用中有一个链接,响应一些 js:

$(function() {

    var $modal = $('<%= escape_javascript( render 'front_end/bookings/modal', :product => @product, :customer => @customer, :booking => @booking ) %>'),
        $calendar = $modal.find('#calendar'),
        currentProduct = <%= raw(@product.to_json) %>;,
        initBookingCalendar;

    $('body').append($modal);

    $modal.modal('show');

    /* Booking Calendar */
    initBookingCalendar = function(el, productId) {

        el.fullCalendar()

        ...

    };

    $($modal).on('shown', function() {
        if($calendar.is(':empty')) {
            initBookingCalendar($calendar, <%= @product.id %>);
        }
    });
});

问题是 'shown' 事件由于某种原因在 IE9 中没有触发!?我在这里重现了这个问题:http://jsfiddle.net/tvkS6/ 并通过这样做:http://jsfiddle.net/tvkS6/9/ 让它工作。问题是我不知道如何在我的代码中实现解决方案,因为我并不真正了解问题所在。

我必须等待 initBookingCalendar 直到模式完全显示的原因是 jQuery FullCalendar 插件要求元素在呈现日历之前可见。

感谢任何提示!

【问题讨论】:

    标签: jquery ruby-on-rails twitter-bootstrap fullcalendar jquery-events


    【解决方案1】:

    您需要在第一次调用 show() 方法之前将监听器的附加移动到。

    $modal.on('shown', function() {
        if($calendar.is(':empty')) {
            initBookingCalendar($calendar, <%= @product.id %>);
        }
    });
    
    $modal.modal('show');
    

    它可能在非 IE 浏览器中工作的原因是因为它们支持 CSS 转换,这使得触发 shown 事件的代码是异步的。由于IE不支持,所以show()方法同步执行,在你附加监听之前触发shown事件。


    更新

    这是一个演示,验证了我对您的示例为何在其他浏览器中有效的解释。通过设置

    $.support.transitions = false;
    

    Modal.show() 方法将在所有浏览器上同步运行,因此,您的示例现在将在 Chrome、FF 等中失败。

    JSFiddle Demo不该做什么

    【讨论】:

    • @JhoonBey,你能澄清一下"this" 是指什么吗?我确实注意到 JSFiddle 不能与 IE 一起使用(因为 IE 不喜欢脚本加载),但我已经纠正了这一点。就这些吗?还是您的行为也有问题?
    猜你喜欢
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多