0

大家晚安!我正在博客上创建一个博客,我想根据标签放置背景颜色。例如:当我搜索一个标签时,例如: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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#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', this, '#D38094')" >Rss  <i class="fas fa-rss"></i></a>
</div>

<!-- <button class="tablink" onclick="openPage('About', this, '#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>

我已经把它放在博主的脚本小工具里了。但是当我打开小工具进行编辑时,上面的部分代码不再出现。但我面临的问题如下,每个选项卡都是一个指向标签的搜索链接,例如在此处输入链接描述。所以我想点击标签,所有带有标签的帖子都matematica将出现在与标签颜色相同的背景中。同样的事情也会发生在所有其他选项卡上。但是当主页面有所有帖子时,即收集帖子时(标签上方的联合操作)背景应该是白色的。

更改背景的代码如下:

<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>


我在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;

再次感谢您的帮助 :)

4

1 回答 1

0

我已经解决了替换tablinks[i].style.backgroundColor = color;to的部分问题,document.body.style.backgroundColor = color; 但它只是在点击的那一刻改变了颜色。我希望背景可以保持颜色,直到我单击另一个选项卡。如果有人知道怎么做:)非常感谢

于 2021-03-16T22:35:59.450 回答