Javascript onFocus最初在Safari中无法触发

时间:2012-02-28 12:57:57

标签: javascript jquery onfocus

我正在这里的客户网站上工作:http://www.marcusleighcopy.co.uk/about

闪光灯弹出后,您应该看到一个关于页面,在图像下面有我建立的推荐设备。用于实际设备的JS很好,但是如果当前没有查看窗口,我想阻止设备做任何事情(当一个奇怪的队列被构建并且当焦点返回到窗口时一下子全部被触发)。

无论如何,我修改了我的代码并添加了这个:(如同在这里的另一个问题)

if (/*@cc_on!@*/false) 
            { 
                // check for Internet Explorer
                document.onfocusin = onFocus;
                document.onfocusout = onBlur;
            } 
            else 
            {
                window.onfocus = onFocus;
                window.onblur = onBlur;
            }

我的推荐设备是这样的:

function onBlur() 
            {
                document.body.className = 'blurred';
            };
            function onFocus(){
                document.body.className = 'focused';
                    // Testimonial fade in/out
                var testimonial_count = $('div.testimonial').size();
                if (testimonial_count)
                {
                    var testimonials = [];

                    $('div.testimonial').each(function(){
                        testimonials.push($(this));
                    });

                    show_testimonials(0);

                }

                function show_testimonials(currentIndex)
                {
                    testimonials[currentIndex].fadeIn(400);

                    if ($('body').hasClass('blurred'))
                    {
                        return false;
                    }

                    setTimeout(function(){

                        testimonials[currentIndex].fadeOut(400);
                        if (currentIndex + 1 >= testimonial_count)
                        {
                            show_testimonials(0);
                        }
                        else
                        {
                            show_testimonials(currentIndex + 1);
                        }

                    }, 10000);
                }
            };

在SAFARI中,窗口最初没有聚焦。如果您单击Safari,然后再单击它,则推荐会按预期显示。如何根据需要让窗口立即触发onFocus功能?

1 个答案:

答案 0 :(得分:1)

这将在DOM ready上触发焦点事件:

$(function(){
     $(document).focus();
};
相关问题