【问题标题】:Conditionally include script for browsers有条件地包含浏览器的脚本
【发布时间】:2015-07-22 20:20:33
【问题描述】:

我有这样的脚本代码:

setTimeout(function () {
    $('.grayscale .inactive img').each(function () {
        $(this).wrap('<div style="display:inline-block;width:' + this.width + 'px;height:' + this.height + 'px;">').clone().addClass('gotcolors').css({
            'position': 'absolute',
            'opacity': 0
        }).insertBefore(this);
        this.src = grayscale(this.src);
    }).animate({opacity: 1}, 500);

    $(".grayscale li").hover(function () {
        $(this).find('.gotcolors').stop().animate({opacity: 1}, 200);
    }, function () {
        $(this).find('.gotcolors').stop().animate({opacity: 0}, 500);
    });
}, 1);`

我在 Mozilla Firefox 中需要它,在 Chrome 中我对这个 grayscale(this.src) 有错误。我怎样才能只为 Mozilla Firefox 包含这个脚本?

【问题讨论】:

    标签: javascript jquery reference conditional-statements jquery-events


    【解决方案1】:

    您可以在不使用任何库的情况下实现此目的。操作方法如下:

    if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
    {
         //Your code
    }
    

    【讨论】:

    • 虽然一般情况下可以使用user agent来检测浏览器软件和版本,但最好使用特征检测,比如Modernizr,这样可以得到更准确的结果。
    • @HayotIsmatov 我很高兴能帮上忙。正如 Raptor 所建议的,您也可以使用 Modernizr。
    【解决方案2】:

    这是我为浏览器检测找到的 JavaScript 库。

    这可能是help

    使用简单:

    // Check if browser is Firefox
    if (bowser.gecko) {
        console.info(bowser.version); // Log browser version
    }
    

    【讨论】:

    • 我想你的意思是bowser(插件名称)而不是bower,对吧?
    • 对不起,错字是bowser
    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多