网站换肤,之前感觉总是很神奇啊,今天就来总结一下。我写的就是两种思路。
首先都需要建一个css文件夹,里面存放不同颜色的css文件:blue.css; red.css; yellow.css; green.css 在这几个文件中分别写好要改变的样式。
接下来就是html文件,首先第一种思路:只写一个link标签(不推荐,原因请继续阅读)。代码如下:
<!--index.html--> <head> <meta charset="UTF-8"> <title>动态换肤</title> <link rel="stylesheet" type="text/css" href="css/blue.css"> <style> *{ margin: 0; padding: 0; } div{ height: 50px; background-color: black; padding-left: 10px; } div section{ width: 30px; height: 30px; margin: 10px; display: inline-block; } div section:nth-of-type(1){ background-color: red; } div section:nth-of-type(2){ background-color: blue; } div section:nth-of-type(3){ background-color: green; } div section:nth-child(4){ background-color: yellow; } </style> </head> <body> <div> <section data-color="red"></section> <section data-color="blue"></section> <section data-color="green"></section> <section data-color="yellow"></section> </div> <script> var div = document.getElementsByTagName("div")[0]; //添加鼠标单击事件 div.onclick = function(event){ console.log(event.target); var ele = event.target; console.log(ele.tagName);//使用.tagName时,控制台输出全部大写,所以在下面的if判断中,使用“SECTION”. if(ele.tagName == 'SECTION'){ var color = ele.dataset.color; //var color = ele.getAttribute("data-color"); var link = document.createElement("link"); link.href = 'css/' + color + ".css"; link.rel = "stylesheet"; // 添加样式表到head,但是会造成页面样式表越来越多,所以不推荐 document.head.appendChild(link); } } </script> </body>
第一种思路是只写一个link,然后不断添加样式表到head,但是会造成页面样式表越来越多,所以不推荐。
第二种思路:写4个link标签,然后通过调节link的可用与否来实现网站换肤。代码如下:
<head> <meta charset="UTF-8"> <title>动态换肤</title> <link rel="stylesheet" type="text/css" href="css/blue.css"> <link rel="stylesheet" type="text/css" href="css/red.css"> <link rel="stylesheet" type="text/css" href="css/green.css"> <link rel="stylesheet" type="text/css" href="css/yellow.css"> <style> *{ margin: 0; padding: 0; } div{ height: 50px; background-color: black; padding-left: 10px; } div section{ width: 30px; height: 30px; margin: 10px; display: inline-block; } div section:nth-of-type(1){ background-color: blue; } div section:nth-of-type(2){ background-color: red; } div section:nth-of-type(3){ background-color: green; } div section:nth-child(4){ background-color: yellow; } </style> </head> <body> <div> <section data-color="0"></section> <section data-color="1"></section> <section data-color="2"></section> <section data-color="3"></section> </div> <script> var links = document.getElementsByTagName('link'); function enableLinks(index){ for(var i = 0;i < links.length; i++){//循环查找4个link标签 //disabled表示关闭,如果i不等于当前index,则disabled就是true,即关闭该link标签 //.sheet表示样式表 links[i].sheet.disabled = i!=index; } } enableLinks(2); //给div标签添加鼠标点击事件 //event:事件对象 document.querySelector('div').onclick = function(event){ var index = event.target.dataset.color; console.log(index); if(index == undefined){ return; }else{ //调用enableLinks() enableLinks(index); } } </script> </body>
注意两种方法的html部分section的自定义属性data-color,一个是颜色,一个是数字。

Here's a container with some child elements:

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.