0

モバイルデバイスを検出し、モバイルデバイスの場合は別のCSSファイルを読み込もうとしています。コードが機能しないのはなぜですか?

    var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};


function getcss(cssfile){

    var loadcss = document.createElement('link')

    loadcss.rel = "stylesheet"

    loadcss.setAttribute("type", "text/css")

    loadcss.setAttribute("href", cssfile)

    document.getElementsByTagName("head")[0].appendChild(loadcss)

}

if(isMobile.any() == true)
// Defines the resolution range you're targeting (less than 800 pixels wide in this case)

{

getcss('cssclass.css')
// Defines the .css file you want to load for this range (800x600.css)

}

else 

{

getcss('css2menuPage.css') 
//This else statement has "if" condition. If none of the following criteria are met, load 1280x1024.css

}
4

1 に答える 1

0

@mediaの画面サイズは、前述のBradのようなものです。それ以外の場合は、ユーザーエージェントのデータベースを設定する必要があります。

@media screen and(max-width:960px){h1、h2 {color:#990000; font-size:1.4em; }}

于 2012-12-10T19:18:09.177 に答える