导航栏中各按钮在点击当前按钮变色其他按钮恢复为原有色的实现方法(vue、jq、原生js)

一、vue如何实现?

代码:

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
<style type="text/css">
li{
list-style: none;
float: left;
margin-left: 5px;
background: yellow;
padding: 1px;
border-radius: 3px;
border:1px solid #999;
}
.active{
color:red;
}
</style>
</head> <body>
<div id="example">
<ul>
<li v-bind:class=" activeClass==index ? 'active' : '' " v-for="(item,index) in list" @click="getItem(index)">{{item.name}}</li>
</ul>
</div>
<script>
var example1 = new Vue({
el: '#example',
data: {
activeClass: -1,
list:[
{"typeId":"1","name":"按钮一","number":"50"},
{"typeId":"2","name":"按钮二","number":"50"},
{"typeId":"3","name":"按钮三","number":"50"},
{"typeId":"4","name":"按钮四","number":"50"},
{"typeId":"5","name":"按钮五","number":"50"},
{"typeId":"6","name":"按钮六","number":"50"},
{"typeId":"7","name":"按钮七","number":"50"},
{"typeId":"8","name":"按钮八","number":"80"},
],
},
watch: { },
methods: {
getItem: function (index) {
this.activeClass = index;
}
}
})
</script>
</body> </html>

效果图:

导航栏中各按钮在点击当前按钮变色其他按钮恢复为原有色的实现方法(vue、jq、原生js)

二、jq如何实现?

代码:

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>刘晓慧</title>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<style type="text/css">
li{
list-style: none;
float: left;
margin-left: 5px;
background: yellow;
padding: 1px;
border-radius: 3px;
border:1px solid #999;
}
.active{
color:red;
}
</style>
</head>
<body>
<div id="example">
<ul>
<li>按钮一</li>
<li>按钮二</li>
<li>按钮三</li>
<li>按钮四</li>
<li>按钮五</li>
<li>按钮六</li>
<li>按钮七</li>
<li>按钮八</li>
</ul>
</div>
</body>
</html>
<script type="text/javascript">
$("li").click(function(){
$("li").removeClass("active");
$(this).addClass("active");
})
</script>

效果图:同上

三、原生js如何实现?

代码:

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
li{
list-style: none;
float: left;
margin-left: 5px;
background: yellow;
padding: 1px;
border-radius: 3px;
border:1px solid #999;
}
.active{
color:red;
}
</style>
</head> <body>
<div id="example">
<ul>
<li>按钮一</li>
<li>按钮二</li>
<li>按钮三</li>
<li>按钮四</li>
<li>按钮五</li>
<li>按钮六</li>
<li>按钮七</li>
<li>按钮八</li>
</ul>
</div>
</body> </html>
<script type="text/javascript">
var buttons=document.getElementsByTagName("li");
var l=buttons.length;
for(var i=0;i<l;i++){
buttons[i].onclick=function(){
for(var j=0;j<l;j++){
if(this==buttons[j]){
this.className="active";
}else{
buttons[j].className="";
} }
}
}
</script>

效果图:同上

代码2(hwt):

 <!DOCTYPE html>
<html>
<head>
<title>练习</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<style>
.btn{background-color:yellow;}
</style>
<script type="text/javascript">
function change(obj){
var btn = document.querySelectorAll('.btn');
for(var i=0;i<btn.length;i++){
btn[i].style.color = "black";
}
document.getElementById(obj).style.color = "red";
}
</script>
<body>
<button type="button" value="" class="btn" onclick="change('a')" id="a">按钮一</button>
<button type="button" value="" class="btn" onclick="change('b')" id="b">按钮二</button>
<button type="button" value="" class="btn" onclick="change('c')" id="c">按钮三</button>
<button type="button" value="" class="btn" onclick="change('d')" id="d">按钮四</button>
<button type="button" value="" class="btn" onclick="change('e')" id="e">按钮五</button>
<button type="button" value="" class="btn" onclick="change('f')" id="f">按钮六</button>
<button type="button" value="" class="btn" onclick="change('g')" id="g">按钮七</button>
<button type="button" value="" class="btn" onclick="change('h')" id="h">按钮八</button>
</body>
</html>
上一篇:JQ添加移除css样式--转载 心存善念


下一篇:项目vue2.0仿外卖APP(四)