search

Home  >  Q&A  >  body text

javascript - JS web page skinning problem

<link href="green.css" rel="stylesheet" type="text/css" />

window.onload = function ()
{
    var oLink = document.getElementsByTagName("link")[0];
    var oSkin = document.getElementById("skin").getElementsByTagName("li");
    
    for(var i = 0; i< oSkin.length; i++)
    {        
        oSkin[i].onclick = function ()
        {
            for(var p in oSkin) oSkin[p].className = "";
            this.className = "current";
            oLink['href'] = this.id + ".css";                
        }    
    }
    
};
<body>
<p id="outer">
    <ul id="skin">
        <li id="red" title="红色">红</li><li id="green" class="current" title="绿色">绿</li><li id="black" title="黑色">黑</li>
    </ul>
    <ul id="nav">
        <li><a href="javascript:;">新闻</a></li>
        <li><a href="javascript:;">娱乐</a></li>
        <li><a href="javascript:;">体育</a></li>
        <li><a href="javascript:;">电影</a></li>
        <li><a href="javascript:;">音乐</a></li>
        <li class="last"><a href="javascript:;">旅游</a></li>
    </ul>
</p>
</body>

What does this.className = "current" mean? Does it correspond to the button that is in the green state on the current page?

世界只因有你世界只因有你2693 days ago880

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-07-05 10:52:04

    this.className = "current";
    Set the class of the currently clicked <li> to current to achieve the purpose of dynamically changing the label style

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-05 10:52:04

    .css should be a style file that loads the specified skin style by id.

    link is an html element used to load resources.

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:52:04

    Generally speaking, skin change will involve a lot of CSS. I usually replace the external CSS to do this function.
    Suppose I now have an HTML file and two HTML skins (two CSS files, one red and one yellow).
    The simple code is as follows
    <!DOCTYPE html>
    <html>
    <head>

    <meta charset="utf-8">
    <title>test</title>
    <link id="css" rel="stylesheet" href="red.css">

    </head>
    <body>

    <button id="red">red</button>
    <button id="yellow">yellow</button>
    <script>
        var css = document.getElementById("css");
        var red = document.getElementById("red");
        var yellow = document.getElementById("yellow");
        red.onclick = function () {
            css.href = "red.css";
        };
        yellow.onclick = function () {
            css.href = "yellow.css";
        };
    </script>

    </body>
    </html>
    That’s probably what it feels like

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 10:52:04

    Your code should be endless. The meaning of this code is probably to modify the skin and dynamically load css styles.

    reply
    0
  • Cancelreply