一种更简单的方法来编写这个onclick背景更改 - HTML / CSS

问题描述 投票:0回答:5

有没有办法简化这段代码?我发现它对于一个简单的功能来说太长了。

我试图使用querySelectorAll(),但我很难尝试重新编码它。

请参阅随附的片段。

我试图寻找一种更简单的方法,不幸的是我无法找到任何方法。

function myFunction() {
  var moreText = document.getElementById("more");
  var btnText = document.getElementById("myBtn");
if (moreText.style.display === "none") {
    moreText.style.display = "inline";
  document.getElementById("more2").style.display = 'none';
  document.getElementById("myBtn").style.backgroundColor = "#000000";
  document.getElementById("myBtn2").style.backgroundColor = "#006668";
} else {
    moreText.style.display = "inline";
  document.getElementById("more2").style.display = 'none';
  document.getElementById("myBtn").style.backgroundColor = "#000000";
  document.getElementById("myBtn2").style.backgroundColor = "#006668";
  } } 
  function myFunction2() {
  var moreText = document.getElementById("more2");
  var btnText = document.getElementById("myBtn2");
if (moreText.style.display === "none") {
    moreText.style.display = "inline";
  document.getElementById("more").style.display = 'none';
  document.getElementById("myBtn").style.backgroundColor = "#006668";
  document.getElementById("myBtn2").style.backgroundColor = "#000000";
} else {
    moreText.style.display = "inline";
  document.getElementById("more").style.display = 'none';
  document.getElementById("myBtn").style.backgroundColor = "#006668";
  document.getElementById("myBtn2").style.backgroundColor = "#000000";
  } } 
   function myFunction3() {
  var moreText = document.getElementById("more3");
  var btnText = document.getElementById("myBtn3");
if (moreText.style.display === "none") {
    moreText.style.display = "inline";
  document.getElementById("more").style.display = 'none';
  document.getElementById("more2").style.display = 'none';
  document.getElementById("myBtn").style.backgroundColor = "#006668";
  document.getElementById("myBtn2").style.backgroundColor = "#006668";
} else {
   moreText.style.display = "inline";
  document.getElementById("more").style.display = 'none';
  document.getElementById("more2").style.display = 'none';
  document.getElementById("myBtn").style.backgroundColor = "#006668";
  document.getElementById("myBtn2").style.backgroundColor = "#006668";
  } } 
div.scrollmenu {
  background-color: #006668;
  overflow: auto;
  white-space: nowrap;
}
#more {display: none;}
#more2 {display: none;}
<div class="scrollmenu">
  <a onclick="myFunction()" id="myBtn" href="#Event1">Event 1</a>
  <a onclick="myFunction2()" id="myBtn2" href="#Event2">Event 2</a>
  </div>
  <span id="more">Display Event Here 1.</span>
<span id="more2">Display Event Here 2.</span>

It's supposed to look like this

html css button hyperlink timeline
5个回答
0
投票

你可以这样做:

const myBtns = document.querySelectorAll(".myBtn");
const descriptions = document.querySelectorAll(".description");

function myFunction(e) {
	myBtns.forEach( btn => {		
		btn.classList.remove("active");
	});
	
	descriptions.forEach( description => {
		description.classList.remove("show");	   
	});
	e.target.classList.add("active");
	document.querySelector("."+e.target.getAttribute("data-show")).classList.add("show");
}
div.scrollmenu {
  background-color: orange;
}
.hide {
	display: none;
}

.show {
	display: block;
}

.active {
	background-color: green;
}
<div class="scrollmenu">
	<a onclick="myFunction(event)" class="myBtn" href="#Event1" data-show="more">Event 1</a>
	<a onclick="myFunction(event)" class="myBtn" href="#Event2" data-show="more2">Event 2</a>
</div>
	<span class="hide description more">Display Event Here 1.</span>
	<span class="hide description more2">Display Event Here 2.</span>

1
投票

请参阅下面更新的示例中的我的评论。

单击时,我们删除按钮和文本区域上的所有活动类,然后将活动类添加到需要它的那些类中。

这里的优点是你没有在javascript中存储活动类。这样,您就不会冒险让表示和存储状态与其他可能正在运行的脚本不同步。

// first we get all the buttons and text areas
var btns = document.querySelectorAll('.btn');
var mores = document.querySelectorAll('.more');

// we add an event listener by looping trough all the buttons
for (const btn of btns) {
  btn.addEventListener('click', handleClick);
}

// handle the button click
function handleClick(event){
  event.preventDefault();
  
  // remove the active class on ALL the buttons
  for (const btn of btns) {
    btn.classList.remove('active');
  }
  // add the active class on the clicked button
  event.target.classList.add('active');
  
  //remove the active class on all the text areas
  for (const more of mores) {
    more.classList.remove('active');
  }
  // here we get the data-show attribute of the clicked element 
  // and use it to show the right text area
  document.querySelector(`#${event.target.dataset.show}`).classList.add('active'); 
}
div.scrollmenu {
  background-color: #006668;
  overflow: auto;
  white-space: nowrap;
}

.btn.active{
  background: #000000;
}

.more{
  display: none;
}

.more.active{
  display: block;
}
<div class="scrollmenu">
  <a class="btn" href="#" data-show="more1" >Event 1</a>
  <a class="btn" href="#" data-show="more2">Event 2</a>
</div>
<span id="more1" class="more">Display Event Here 1.</span>
<span id="more2" class="more">Display Event Here 2.</span>

0
投票

使用下面的javascript,你可以使它更简单

function myFunction(event) {
  var href = event.target.href.substring(event.target.href.indexOf('#') + 1);
  var id = event.target.id;
  var x = document.getElementsByTagName("span");
  var y = document.getElementsByTagName("a");
  var i,j;
  for (i = 0; i < x.length; i++) {
    x[i].style.display = 'none';
  }
  for (j = 0; j < y.length; j++) {
    y[j].style.backgroundColor = "#006668";
  }
  document.getElementById(href).style.display = 'inline';
  document.getElementById(id).style.backgroundColor = "#000000";
}
div.scrollmenu {
  background-color: #006668;
  overflow: auto;
  white-space: nowrap;
}
span {display: none;}
<div class="scrollmenu">
  <a onclick="myFunction(event)" id="myBtn" href="#Event1">Event 1</a>
  <a onclick="myFunction(event)" id="myBtn2" href="#Event2">Event 2</a>
  <a onclick="myFunction(event)" id="myBtn3" href="#Event3">Event 3</a>
  <a onclick="myFunction(event)" id="myBtn4" href="#Event4">Event 4</a>
  <a onclick="myFunction(event)" id="myBtn5" href="#Event5">Event 5</a>
  <a onclick="myFunction(event)" id="myBtn6" href="#Event6">Event 6</a>
  <a onclick="myFunction(event)" id="myBtn7" href="#Event7">Event 7</a>
  <a onclick="myFunction(event)" id="myBtn8" href="#Event8">Event 8</a>
  <a onclick="myFunction(event)" id="myBtn9" href="#Event9">Event 9</a>
  <a onclick="myFunction(event)" id="myBtn10" href="#Event10">Event 10</a>
</div>
<span id="Event1">Display Event Here 1.</span>
<span id="Event2">Display Event Here 2.</span>
<span id="Event3">Display Event Here 3.</span>
<span id="Event4">Display Event Here 4.</span>
<span id="Event5">Display Event Here 5.</span>
<span id="Event6">Display Event Here 6.</span>
<span id="Event7">Display Event Here 7.</span>
<span id="Event8">Display Event Here 8.</span>
<span id="Event9">Display Event Here 9.</span>
<span id="Event10">Display Event Here 10.</span>

-2
投票

.px-0 {
      padding-left: 0 !important;
      padding-right: 0 !important;
    }
    ul.nav.nav-tabs {
    	background-color: #006668;
    }
    ul.nav.nav-tabs li a,ul.nav.nav-tabs li a:hover {
    	color: #fff;
        border: 0;
        background-color: transparent;
    }
    ul.nav.nav-tabs li.active a,ul.nav.nav-tabs li.active a:hover {
    	background-color: #000;   
        border: 0;
        border-radius: 0;
        color: #fff;
    }
    .tab-content {
    	padding: 20px;
    }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="container-fluid px-0">
  

  <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#home">Event 1</a></li>
    <li><a data-toggle="tab" href="#menu1">Event 2</a></li>
    <li><a data-toggle="tab" href="#menu2">Event 3</a></li>
    <li><a data-toggle="tab" href="#menu3">Event 4</a></li>
  </ul>

  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <p>Display Event Here 1.</p>
    </div>
    <div id="menu1" class="tab-pane fade">
      <p>Display Event Here 2.</p>
    </div>
    <div id="menu2" class="tab-pane fade">
      <p>Display Event Here 3.</p>
    </div>
    <div id="menu3" class="tab-pane fade">
      <p>Display Event Here 4.</p>
    </div>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.