2

好的 StackOverflow - 这是一个奇怪的。

问题

所以我有一个“按钮”(实际上只是一个带有 javascript onclick 侦听器的 div),它在页面加载时通过 json 从数据库中获取其文本。(考虑到应用程序,这是有道理的)。我们不支持 IE < 9。

问题是 div 内的文本没有显示在 IE9 的新实例中。然而,当我打开开发者控制台(试图找出问题所在)并刷新页面时,IE 表现得好像没有任何问题,并正确填充按钮文本。它适用于所有其他浏览器。

什么。

编码

所以这就是我在 PHP 中所拥有的:

<div class="dark-button notification-button" data-notification="NOTIF_A">
    <span class="float-right">&#9654;</span>
</div>

Javascript(使用 Jquery 1.8.0):

$('document').ready(refreshNotificationButtons('.notification-button'));
function refreshNotificationButtons(buttons){
    var buttons = typeof buttons != 'object' ? $('.notification-button') : buttons;
    var allNotifications = {};
    var buttonsCount = 0;
    buttons.each(function(){
        var button = $(this);
        if(typeof button.attr('id') == 'undefined') button.attr('id', 'notif-button-' + buttonsCount);
        buttonsCount ++;
        if(typeof allNotifications[button.attr('data-notification')] == 'undefined'){
            allNotifications[button.attr('data-notification')] = [button.attr('id')];
        } else {
            allNotifications[button.attr('data-notification')].push(button.attr('id'))
        }
    });
    $.get(
        '/notifications/get_notifications/' + $.map(allNotifications, function(x, abbr){return abbr}).join(','),
        '',
        function(data){
            $(data).each(function(){
                if (typeof allNotifications[this.Notification.NotificationAbbr] != 'undefined'){
                    console.log(this); //debug
                    buttonEl = $('#' + allNotifications[this.Notification.NotificationAbbr]);
                    if(this.Notifications_User.UserID != null) buttonEl.addClass('notification-button-remove');
                    buttonEl.attr('data-notification-id', this.Notification.Notifications_TableID);
                    buttonEl.append($('<span class="notification-button-signup-span">' + this.Notification.EnableText + '</span><span class="notification-button-remove-span">' + this.Notification.DisableText + '</span>'));
                }
            });
        },
        'json'
    );

}

图片

打开开发控制台之前的按钮:

打开开发控制台之前的按钮

打开开发控制台并刷新后的按钮:

打开开发控制台后的按钮

结论

有任何想法吗?如果您想查看,我很乐意发布更多代码!提前致谢。

4

2 回答 2

5

如上所述,console.log()当没有控制台可以写入时,在 IE 中效果不佳。

您可以删除对 的调用console,或添加此插件(由 Paul Irish 开发)

http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

于 2012-08-13T21:57:38.197 回答
2

IE 只有console在调试器处于活动状态时才有对象。

因此,当您尝试执行console.log()并且没有console对象时,它会引发 javascript 异常,因为它尝试引用的.log属性undefined并且您的代码停止执行。

你可以通过在你的初始化代码中插入这个来解决这个问题——在你使用之前的某个地方console.log()

var console = console || {};
if (!console.log) {
    console.log = function(data) {
        // do nothing
    }
}

console.log()即使在调试器未运行时在 IE 中,这也使得始终使用它是安全的。我不建议console.log()在完成的生产代码中保留语句,但它可能会在开发过程中对您有所帮助。

于 2012-08-13T22:09:49.120 回答