简体   繁体   中英

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

I have a JS function that should get a stylesheet element for CSS swap. When running the page on IPHONE or Desktop Version Chrome and Safari I am able successfully to swap the css.

My problem is on Android Browser where

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

from my code return FALSE.

It seems that on

Android Web Browser a.getAttribute("rel))" : stylesheet element is not present.

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. make sure that the rel attribute is present before you access it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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