简体   繁体   English

如果设备上存在键盘,则加载其他样式表(javascript检测)

[英]Load different stylesheet if keyboard is present on device (javascript detect)

I am trying to load a different CSS layout in case there is a physical keyboard on the device (q10.css). 我正在尝试加载其他CSS布局,以防设备上有物理键盘(q10.css)。 In case there is no keyboard, the default one is used (normalCss). 如果没有键盘,则使用默认键盘(normalCss)。 For some reason the stylesheets are not loaded: 由于某些原因,样式表未加载:

<script>
    var normalCss = document.createElement("<link rel=\"stylesheet\" href=\"css/main.css\" />");
    var q10Css = document.createElement("<link rel=\"stylesheet\" href=\"css/q10.css\" />");

    if (window.navigator.userAgent.indexOf("Kbd") != -1) {
        document.getElementsByTagName("head")[0].appendChild(q10Css);
            }
    else {
        document.getElementsByTagName("head")[0].appendChild(normalCss);
            }
</script>

*any help is highly appreciated :) Thank you! *非常感谢您的帮助:)谢谢!

That's not how you create elements, you have to do it like this : 这不是创建元素的方式,您必须这样做:

var normalCss = document.createElement('link');
normalCss.href = 'css/main.css';
normalCss.setAttribute('rel', 'stylesheet');

如您所见 createElement仅解析tagName而不解析文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM