使用浏览器检测和body_class()函数修改CSS

时间:2012-04-01 作者:keruilin

我正在使用以下函数,如下所述:

http://www.nathanrice.net/blog/browser-detection-and-the-body_class-function/

现在,当我使用以下css样式时,它完全符合我在Safari和Chrome中的要求:

.safari li#linkrss {margin-top: -15px;}
.chrome li#linkrss {margin-top: -15px;}
当我尝试时.gecko li#linkrss {margin-top: 0px;} 然而,对于Firefox来说,这似乎并不适用。例如,当我使用FireBug检查css元素时,它显示了Safari的样式。关于如何解决这个问题,有什么建议吗?

1 个回复
最合适的回答,由SO网友:Sagive 整理而成

Can\'t tell you exactly why but that function didnt work for me either..
Finally i have found this script i have modified a little bit here: get_browser()

下面是(在functions.php中):

function whatBrowser() {
    //browsers
    define("UNKNOWN", 0);
    define("TRIDENT", 1);
    define("GECKO", 2);
    define("PRESTO", 3);
    define("WEBKIT", 4);
    define("VALIDATOR", 5);
    define("ROBOTS", 6);

    if(!isset($_SESSION["info"][\'browser\'])) {
        $_SESSION["info"][\'browser\'][\'engine\'] = UNKNOWN;
        $_SESSION["info"][\'browser\'][\'version\'] = UNKNOWN;
        $_SESSION["info"][\'browser\'][\'platform\'] = \'Unknown\';

        $navigator_user_agent = \' \' . strtolower($_SERVER[\'HTTP_USER_AGENT\']);

        if (strpos($navigator_user_agent, \'linux\')) :
            $_SESSION["info"][\'browser\'][\'platform\'] = \'Linux\';
        elseif (strpos($navigator_user_agent, \'mac\')) :
            $_SESSION["info"][\'browser\'][\'platform\'] = \'Mac\';
        elseif (strpos($navigator_user_agent, \'win\')) :
            $_SESSION["info"][\'browser\'][\'platform\'] = \'Windows\';
        endif;

        if (strpos($navigator_user_agent, "trident")) {
            $_SESSION["info"][\'browser\'][\'engine\'] = TRIDENT;
            $_SESSION["info"][\'browser\'][\'version\'] = floatval(substr($navigator_user_agent, strpos($navigator_user_agent, "trident/") + 8, 3));
        }
        elseif (strpos($navigator_user_agent, "webkit")) {
            $_SESSION["info"][\'browser\'][\'engine\'] = WEBKIT;
            $_SESSION["info"][\'browser\'][\'version\'] = floatval(substr($navigator_user_agent, strpos($navigator_user_agent, "webkit/") + 7, 8));
        }
            elseif (strpos($navigator_user_agent, "presto")) {
            $_SESSION["info"][\'browser\'][\'engine\'] = PRESTO;
            $_SESSION["info"][\'browser\'][\'version\'] = floatval(substr($navigator_user_agent, strpos($navigator_user_agent, "presto/") + 6, 7));
        }
        elseif (strpos($navigator_user_agent, "gecko")) {
            $_SESSION["info"][\'browser\'][\'engine\'] = GECKO;
            $_SESSION["info"][\'browser\'][\'version\'] = floatval(substr($navigator_user_agent, strpos($navigator_user_agent, "gecko/") + 6, 9));
        }
        elseif (strpos($navigator_user_agent, "robot"))
            $_SESSION["info"][\'browser\'][\'engine\'] = ROBOTS;
        elseif (strpos($navigator_user_agent, "spider"))
            $_SESSION["info"][\'browser\'][\'engine\'] = ROBOTS;
        elseif (strpos($navigator_user_agent, "bot"))
            $_SESSION["info"][\'browser\'][\'engine\'] = ROBOTS;
        elseif (strpos($navigator_user_agent, "crawl"))
            $_SESSION["info"][\'browser\'][\'engine\'] = ROBOTS;
        elseif (strpos($navigator_user_agent, "search"))
            $_SESSION["info"][\'browser\'][\'engine\'] = ROBOTS;
        elseif (strpos($navigator_user_agent, "w3c_validator")) 
            $_SESSION["info"][\'browser\'][\'engine\'] = VALIDATOR;
        elseif (strpos($navigator_user_agent, "jigsaw"))
            $_SESSION["info"][\'browser\'][\'engine\'] = VALIDATOR;

    echo $_SESSION[\'browser\'][\'engine\'];
        switch($_SESSION["info"][\'browser\'][\'engine\']) {
            case UNKNOWN: echo "unknown";
            break;
            case TRIDENT: echo "trident";
            break;
            case GECKO: echo "gecko";
            break;
            case PRESTO: echo "presto";
            break;
            case WEBKIT: echo "Webkit";
            break;
            case VALIDATOR: echo "validator";
            break;
            case ROBOTS: echo "robot";
        }
    }
}

Then just change the body tag to this:

<body class="<?php whatBrowser(); ?>">

我用铬合金测试了它;Firefox。。。为我工作过gr8。。

希望这有帮助<祝你好运,萨吉夫。

结束