link rel alternate实现网站换肤功能实例页面

回到相关文章 »

效果:

背景色是?文字颜色是?

选择样式:

代码:

CSS代码:
/* default.css中 */
.box {
    outline: 5px solid;
    outline-offset: -5px;
}
/* red.css中 */
.box {
    background-color: #cd0000;
    color: #fff;
}
/* gren.css中 */
.box {
    background-color: green;
    color: orange;
}
                
HTML代码:
<link href="reset.css" rel="stylesheet" type="text/css">
                
<link href="default.css" rel="stylesheet" type="text/css" title="默认">
<link href="red.css" rel="alternate stylesheet" type="text/css" title="红色">
<link href="green.css" rel="alternate stylesheet" type="text/css" title="绿色">

<div class="box">
    <img src="../201808/book.jpg">
    <p>背景色是?文字颜色是?</p>
</div>
<p>
    选择样式:
    <input id="default" type="radio" name="skin" value="default.css" checked><label for="default">默认</label>
    <input id="red" type="radio" name="skin" value="red.css"><label for="red">红色</label>
    <input id="green" type="radio" name="skin" value="green.css"><label for="green">绿色</label>
</p>
                
JS代码:
var eleLinks = document.querySelectorAll('link[title]');
var eleRadios = document.querySelectorAll('input[type="radio"]');
[].slice.call(eleRadios).forEach(function (radio) {
    radio.addEventListener('click', function () {
        var value = this.value;
        [].slice.call(eleLinks).forEach(function (link) {
            link.disabled = true;
            if (link.getAttribute('href') == value) {
                link.disabled = false;
            }
        });
    });
});