Changing stylesheet programatically not applying n

2019-09-21 10:47发布

I'm trying to implement the function for a User to change style of the application.

Index.html

<!DOCTYPE html>
<html>
    <head>
        <link id="style" rel="stylesheet" type="text/css" href="red-stylesheet.css" />
    </head>
</html>

Javascript

function swapStyle(sheet) {
    document.getElementById('style').setAttribute('href', sheet);
}

Where sheet = either blue-stylesheet.css or red-stylesheet.css.

It doesn't seem to work. My default theme is red, but when trying to change to blue, ALL styling seems to just disappear, and I can't get the red theme back.

How does one proceed?

(PhoneJS/DevExtreme application)

Default when app is initiated

enter image description here

After pressing "Blue"

enter image description here

2条回答
Evening l夕情丶
2楼-- · 2019-09-21 11:14

Hope this helps :

$('head>link[type="text/css"]').attr('href',sheet);
查看更多
\"骚年 ilove
3楼-- · 2019-09-21 11:20
-----------Try This  -----
You use this code it's work

<!DOCTYPE html>
<html>
<head>
<link id="pagestyle" rel="stylesheet" type="text/css" href="default.css">
<script>
function swapStyleSheet(sheet){
    document.getElementById('pagestyle').setAttribute('href', sheet);
}
</script>
</head>
<body>
<h2>Javascript Change StyleSheet Without Page Reload</h2>
<button onclick="swapStyleSheet('dark.css')">Dark Style Sheet</button>
<button onclick="swapStyleSheet('default.css')">Default Style Sheet</button>
</body>
</html>
查看更多
登录 后发表回答