点击li标签,被点击的li字体变色
方法一:
<li @click=g(this.id) id="m">month</li>
<li @click=g(this.id) id="y">year</li>
<li @click=g(this.id) id="w">week</li>
g(x) {
d = document.getElementsByTagName('li')
for ( let q = 0; q < d.length; q++) {
if(d[p].id!=x){d[p].style.backgroundColor='#FFFFFF'/*其他*/}
else{d[p].style.backgroundColor='#D2D2D2'/*点击的*/}
}
}
方法二:
<html>
<head>
<style type="text/css">
ul{
float:left;
width:100%;/*ul 元素的宽度是 100%*/
padding:0;
margin:0;
list-style-type:none;/*去除无序列表前面的小点*/
}
a{
float:left;
width:7em;/*列表中的每个超链接的宽度是 7em(当前字体尺寸的 7 倍)*/
text-decoration:none;/*没有文本装饰*/
color:white;
background-color:lightpink;/*光标未在鼠标上时,显示的颜色*/
padding:0.2em 0.6em;
border-right:1px solid white;/*添加颜色和边框,以使其更漂亮。*/
}
a:hover {background-color:gray;}/*光标在标签上时,显示另一种颜色*/
li{display:inline;}
/*把li标签变成行内元素,元素前后没有换行,这样就可以使列表排列成一行。*/
</style>
</head>
<body>
<ul>
<li><a href="#">Link one</a></li><!-- href="#"默认跳转到当前页面 -->
<li><a href="#">Link two</a></li>
<li><a href="#">Link three</a></li>
<li><a href="#">Link four</a></li>
</ul>
<p>
</p>
</body>
</html>
方法三:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.active {
background-color: cadetblue;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for='(i,index) in message' @click="btnClick(index)" :class="{active:currentIndex==index}">{{index}}-{{i}}
</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
currentIndex: 0,
message: ['PUBG', 'LOL', 'CF']
},
methods: {
btnClick: function (index) {
this.currentIndex = index;
}
}
});
</script>
</body>
</html>
方法四:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="../Js/vue.min.js"></script>
<style >
.liClinkClass {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!--开始时第一条红色 点击谁谁变成红色-->
<!--展示列表-->
<ul>
<li v-for="( m , index ) in movies"
v-on:click="li_click(index, m)"
v-bind:class="{ 'liClinkClass' : isoKIndex == index }">{{ index + 1 }} - {{ m }} </li>
</ul>
</div>
<script>
const app = new Vue({
el:"#app",
data:{
movies:[ "电影一" , "电影二" , "电影三" , "电影四" , "电影五" ],
isoKIndex:0,
},
methods:{
li_click : function(data,item){
this.isoKIndex = data;
console.log("点击了:"+item);
}
}
});
</script>
</body>
</html>