Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

javascript - 用JS更改样式表[重复](CHange style sheet with JS [duplicate])

I would like a script in JavaScript to change the active style sheet at the click of a button.

(我希望JavaScript中的脚本可以通过单击按钮来更改活动样式表。)

My code is as follows:

(我的代码如下:)

index.html file :

(index.html文件:)

    <head>
    <script type="application/javascript" src="javascript.js">
    <link id="pagestyle" href="default.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <button id="stylesheet1" onclick="initate();"> Default Style Sheet </button>
    <button id="stylesheet2" onclick='onclick="initate();"'> Dark Style Sheet </button>
</body>

javascript.js file :

(javascript.js文件:)

 function swapStyleSheet(sheet) {
        document.getElementById("pagestyle").setAttribute("href", sheet);  
    }

    function initate() {
        var style1 = document.getElementById("stylesheet1");
        var style2 = document.getElementById("stylesheet2");

        style1.onclick = swapStyleSheet("default.css");
        style2.onclick = swapStyleSheet("dark.css");
    }

default.css :

(default.css:)

   body{
        background-color: white;
        color: black
    }

and, at last, dark.css :

(最后是dark.css:)

    body{
        background-color: black;
        color: white
    }

but it doesn't work.

(但这不起作用。)

Can you help me?

(你能帮助我吗?)

Thank you.

(谢谢。)

  ask by MathiAs2Pique_ translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...