gpt4 book ai didi

javascript - Vuejs 突出显示项目列表中单击项目的颜色?

转载 作者:搜寻专家 更新时间:2023-10-30 22:21:33 26 4
gpt4 key购买 nike

当特定项目被点击时,我们如何在项目列表中高亮显示一个项目?我们应该使用 id 作为引用吗?

<li v-for="todo in todos">
<label>
<a href="#"
v-on:click="toggle(todo)"
:style="{color:activeColor}"
>


{{ todo.text }}

</a>

</label>
</li>
toggle: function(todo){

this.activeColor = 'red'
}

我在这里试过: https://jsfiddle.net/eywraw8t/110976/

最佳答案

您可以添加activeIndex 来存储当前的事件索引:

<div id="app">
<h2>Todos:</h2>
<ol>
<li v-for="(todo, idx) in todos">
<label>
<a href="#"
v-on:click="toggle(idx)"
v-bind:checked="todo.done"
:class="{'active': idx == activeIndex}"
>
{{ todo.text }}

</a>

</label>
</li>
</ol>
</div>

new Vue({
el: "#app",
data: {
activeColor:String,
todos: [
{ text: "Learn JavaScript", done: false },
{ text: "Learn Vue", done: false },
{ text: "Play around in JSFiddle", done: false },
{ text: "Build something awesome", done: false }
],
activeIndex: null
},
methods: {
toggle: function(index){
this.activeIndex = index
}
}

在CSS中

.active {
color: red;
}

演示:https://jsfiddle.net/Lv7eanru/

关于javascript - Vuejs 突出显示项目列表中单击项目的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51035543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com