问题描述
各位晚安!
我正在博客上创建一个博客,我想根据标签放置背景颜色。
例如:当我搜索一个标签例如:Books
时,所有包含此标签的帖子都会出现,而包含此标签的帖子将以蓝色背景显示。当我打开带有 Books
标签的帖子时,该帖子也必须具有蓝色博客背景。
我试图在英文的 stackoverflow 中找到一个类似的问题,但代码在这里不起作用是链接 Change color by Blogger Tag
但是当我将此代码放在博客上并使用具有适当颜色的标签时,它始终保留最后一组颜色,而不是我想要的每个标签的颜色...
当没有标签时,他会以与没有 if
相同的方式放置颜色。
博主应该保持在侧面每个导航按钮的颜色之上。
当我放置代码时,主页是只有当所有帖子都包含我正在寻找的标签时才应该使用的颜色。
这是主页面
<!-- https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sidenav_dropdown -->
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/fontawesome.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/solid.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/brands.min.css"/>
<!doctype html>
<style>
#mySidenav a {
position: fixed;
left: -230px;
transition: 0.3s;
padding: 15px;
width: 270px;
text-decoration: none;
font-size: 20px;
color: white;
border-radius: 0 5px 5px 0;
}
#mySidenav a:hover {
left: 0;
}
#search {
top: 40px;
background-color: #FFBA99;
}
#home {
top: 100px;
background-color: #BA2D26;
}
#about {
top: 160px;
background-color: #0055A5;
}
#blog {
top: 220px;
background-color: #5F6BB0;
}
#projects {
top: 280px;
background-color: #158DA9;
}
#contact {
top: 340px;
background-color: #8F2054;
}
#matematica{
top:400px;
background-color:#B1BB36;
}
#sheatsheet{
top:460px;
background-color:#B80c00;
}
#postsPolulares{
top:520px;
background-color:#420085;
}
#arquivos{
top:580px;
background-color:#963c00;
}
#contato{
top:640px;
background-color:#8a0a83;
}
#sobre{
top:700px;
background-color:#006b54;
}
#repositorio{
top:760px;
background-color:#000000;
}
#calendario{
top:820px;
background-color:#0F89BA;
}
#media{
top:880px;
background-color:#EC7650;
}
#books{
top:940px;
background-color:#49497F;
}
#RSS{
top:1000px;
background-color:#D38094;
}
a{
text-align: right;
}
/* Novo estilo*/
*{Box-sizing: border-Box}
/* Set height of body and the document to 100% */
body,html {
height: 100%;
margin: 0;
font-family: Arial;
}
/* Style tab links */
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
width: 25%;
}
.tablink:hover {
background-color: #777;
}
/* Style the tab content (and add height:100% for full page content) */
.tabcontent {
color: white;
display: none;
padding: 100px 20px;
height: 100%;
}
#Search {background-color: #FFBA99;}
#Home {background-color: #BA2D26;}
#About {background-color: #0055A5;}
#Blog {background-color: #5F6BB0;}
#Projects {background-color: #158DA9;}
#Contact {background-color: #8F2054;}
#Matematica {background-color:#B1BB36;}
#Sheatsheet {background-color:#B80c00;}
#PostsPolulares {background-color:#420085;}
#Arquivos {background-color:#963c00;}
#Contato {background-color:#8a0a83;}
#Sobre {background-color:#006b54;}
#Repositorio {background-color:#000000;}
#Calendario {background-color:#0F89BA;}
#Media {background-color:#EC7650;}
#Books {background-color:#49497F;}
#RSS {background-color:#D38094;}
/*#News {background-color: green;}
#Contact {background-color: blue;}
#About {background-color: orange;}*/
/*FIm DO NOVO ESTILO*/
</style>
<div id="mySidenav" class="sidenav">
<a href="https://www.steamizando.com/p/busca.html" id="search" class="tablink" onclick="openPage('Search',this,'#FFBA99')">Pesquisar <i class="fas fa-search"></i></a>
<a href="https://www.steamizando.com/p/home.html" id="home" class="tablink" onclick="openPage('Home','#BA2D26')">Home <i class="fas fa-home"></i></a>
<a href="https://www.steamizando.com/p/ciencia.html" id="about" class="tablink" onclick="openPage('About','#0055A5')"> Ciência <i class="fas fa-microscope"></i> </a>
<a href="https://www.steamizando.com/p/tecnologia.html" id="blog" class="tablink" onclick="openPage('Blog','#5F6BB0')"> Tecnologia <i class="fas fa-laptop-code"></i></a>
<a href="https://www.steamizando.com/p/engenharia.html" id="projects" class="tablink" onclick="openPage('Projects','#158DA9')"> Engenharia <i class="fas fa-tools"></i></a>
<a href="https://www.steamizando.com/p/artes.html" id="contact" class="tablink" onclick="openPage('Contact','#8F2054')"> Artes <i class="fas fa-palette"></i></a>
<a href="https://www.steamizando.com/search/label/Matematica" id="matematica" class="tablink" onclick="openPage('Matematica','#B1BB36')"> Matematica <i class="fas fa-square-root-alt"></i></a>
<a href="https://www.steamizando.com/p/cheatsheet.html" id="sheatsheet" class="tablink" onclick="openPage('Sheatsheet','#b80c00')" >Sheatsheet <i class="fas fa-table"></i></a>
<a href="https://www.steamizando.com/p/populares.html" id="postsPolulares" class="tablink" onclick="openPage('PostsPolulares','#420085')" > Polulares <i class="fas fa-fire"></i></a>
<a href="https://www.steamizando.com/p/arquivos.html" id="arquivos" class="tablink" onclick="openPage('Arquivos','#963c00')" >Arquivos <i class="fas fa-folder"></i></a>
<a href="https://www.steamizando.com/p/contato.html" id="contato" class="tablink" onclick="openPage('Contato','#8a0a83')" >Contato <i class="fas fa-at"></i></a>
<a href="https://www.steamizando.com/p/sobre.html" id="sobre" class="tablink" onclick="openPage('Sobre','#006b54')" >Sobre <i class="fas fa-address-card"></i></a>
<a href="https://www.steamizando.com/p/repositorio.html" id="repositorio" class="tablink" onclick="openPage('Repositorio','#000000')" >Repositorio <i class="fab fa-github-alt"></i></a>
<a href="https://www.steamizando.com/p/calendario.html" id="calendario" class="tablink" onclick="openPage('Calendario','#0F89BA')" >Calendario <i class="fas fa-calendar-alt"></i></a>
<a href="https://www.steamizando.com/p/media.html" id="media" class="tablink" onclick="openPage('Media','#EC7650')">Media <i class="fas fa-photo-video"></i></a>
<a href="https://www.steamizando.com/p/livros.html" id="books" class="tablink" onclick="openPage('Books','#49497F')"> Books <i class="fas fa-book"></i></a>
<a href="https://www.steamizando.com/p/RSS.html#" id="RSS" class="tablink" onclick="openPage('RSS','#D38094')" >RSS <i class="fas fa-RSS"></i></a>
</div>
<!-- <button class="tablink" onclick="openPage('About','#0055A5')">Home</button> -->
<div id="Search" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Home" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="About" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Blog" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Projects" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Contact" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Matematica" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Sheatsheet" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="PostsPolulares" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Sheatsheet" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Arquivos" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Contato" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Repositorio" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Sobre" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Media" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Calendario" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="Books" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<div id="RSS" class="tabcontent">
<h3>Home</h3>
<p>Home is where the heart is..</p>
</div>
<script>
function openPage(pageName,elmnt,color) {
var i,tabcontent,tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = color;
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = color;
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
</!doctype>
我已将其放入博客的脚本小工具中。但是当我打开小工具进行编辑时,上面的部分代码不再出现在那里。
但我面临的问题如下,对于每个 Tab 将是一个指向 enter link description here 等标签的搜索链接。所以我想点击标签,所有带有 matematica
标签的帖子都将出现在与标签颜色相同的背景中。同样的事情也会发生在所有其他标签上。但是当主页面有所有帖子的时候,也就是集合帖子的时候(标签上面的union操作)背景应该是白色的。
更改背景的代码如下:
<script>
function openPage(pageName,tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = color;
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = color;
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
我在 Change color by Blogger Tag 中找到的那个
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = color;
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = color;
再次感谢您的帮助:)
解决方法
我已经解决了部分问题,将 tablinks[i].style.backgroundColor = color;
替换为 document.body.style.backgroundColor = color;
但它只是在点击的那一刻改变了颜色。
我希望背景可以保持颜色,直到我点击另一个选项卡。
如果有人知道怎么做:)
非常感谢