简体   繁体   English

如何交换CSS-无法获取元素样式表[object HTMLLinkElement]

[英]How to swap CSS - Cannot get element stylesheet [object HTMLLinkElement]

I have a JS function that should get a stylesheet element for CSS swap. 我有一个JS函数,应该为CSS交换获取样式表元素。 When running the page on IPHONE or Desktop Version Chrome and Safari I am able successfully to swap the css. 在IPHONE或桌面版Chrome和Safari上运行页面时,我能够成功交换CSS。

My problem is on Android Browser where 我的问题是在Android浏览器上

if (a.getAttribute("rel").indexOf("style") != -1) {

from my code return FALSE. 从我的代码返回FALSE。

It seems that on 看来

Android Web Browser a.getAttribute("rel))" : stylesheet element is not present. Android Web浏览器a.getAttribute("rel))" : stylesheet元素不存在。

Any idea why this behaviour and how to solve it? 任何想法为什么这种行为以及如何解决呢?

// Swap CSS
changeStyle: function(state) {       
    var i, a, url, i = 0;
    for (i; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1) {
            url = a.href;
            if (state == 'accessible') {
                a.href = 'resources/css/app-accesibility.css';;
            }
            else if (state == 'normal') {
                a.href = 'resources/css/app.css';
            }
        }

    }

}

It may be that other link elements are added to the head on android, by plugins or whatever. 可能是其他链接元素通过插件或其他方式添加到了android的头部。 make sure that the rel attribute is present before you access it: 在访问它之前,请确保存在rel属性:

if (a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1) {

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

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