[英]How to change active class on li element in JQuery and redirect the user to the specified page when li elelment is clicked?
[英]VueJs set active class to the data coming from API, when one li element is clicked in V-for loop
我需要將活動類設置為來自 API 的眾多選項之一。 此外,第一個選項首先處於活動狀態,當用戶單擊時,它必須切換。 以下是我的模板:
<div @click="showTimezone">
<span>{{defaultCountry}}</span>
</div>
<div id="timezone-menu" class="timezone-menu" v-show="showTimezone">
<ul >
<li class="country-menu-item" v-for="item in Options" :id="item.id" :key="item.abbrevation" @click="selectCountry(item)">
{{ item.optionText }}
</li>
</ul>
</div>
我將 API 中的數據存儲在 Options 數組中。
data() {
return {
defaultCountry: "CST",
Options: [],
}
}
在下拉菜單中,CST 默認為粗體。 單擊其他選項時,它應該會丟失其樣式類並添加到用戶單擊的其他選項中。 請幫助我了解如何執行此操作。 如果需要,將提供其他詳細信息。
將名為activeId
屬性添加到您的數據對象並在selectCountry
方法中更新它:
data() {
return {
defaultCountry: "CST",
Options: [],
activeId:-1
}
},
methods:{
selectCountry(item){
...
this.activeId=item.id;
}
}
在模板中添加類綁定:
<li class="country-menu-item" :class="{'active':item.id===activeId}" v-for="item in Options"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.