鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ vue點(diǎn)擊li添加class名字問題,求指導(dǎo)

vue點(diǎn)擊li添加class名字問題,求指導(dǎo)

我這樣寫有點(diǎn)小問題,點(diǎn)擊權(quán)限管理打開后點(diǎn)擊部分管理1變色后,再點(diǎn)開電池管理下面的部門管理2也變色了,是因為循環(huán)ul的時候索引的問題嗎,求指導(dǎo)請輸入代碼

clipboard.png

clipboard.png

clipboard.png

clipboard.png

請輸入代碼
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8">  
    <title>vue點(diǎn)擊切換顯示隱藏</title>  
    <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>  
    <style type="text/css">  
    *{  
        padding: 0;  
        margin: 0;  
        font-size: 14px;  
    }  
    ul{  
        width: 200px;  
        height: auto;  
    }  
    h2{  
        background: green;  
        border: 1px solid #fff;  
        color: #fff;  
        height: 30px;  
        line-height: 30px;  
        text-indent: 24px;  
    }  
    h3{  
        background: #999;  
        height: 24px;  
        line-height: 24px;  
        border: 1px solid #fff;  
        text-indent: 50px;  
    }  
    a{
        display: block;
        width: 200px;
    }
    .zhe li.actives{
        color: #fff;
        background: red;
    }
    </style>  
</head>  
<body>  
    <div id="app">
    <ul>
        <li v-for="menu in menus" style="background:#ccc;margin:10px 0">
            <a  @click="toggle(menu)">{{menu.name}}</a>
            <ul v-show="menu.open" style="background:green;" class="zhe">
                <li v-for="(item,index) in menu.list" @click = "abc(index)" :class="{'actives':i==index}">{{item.name}}</li>
            </ul>
        </li>
    </ul>
</div>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                     i:null,
                menus: [
                    {
                        name: '權(quán)限管理',
                        open: false,
                        id:'aaa',
                        list: [
                            {
                                name: '用戶管理1'
                            },
                            {
                                name: '部門管理1'
                            },
                            {
                                name: '角色管理1'
                            },
                            {
                                name: '菜單管理1'
                            }
                        ]
                    },
                    {
                        name: '電池管理',
                        open: false,
                        id:'bbb',
                        list: [
                            {
                                name: '用戶管理2'
                            },
                            {
                                name: '部門管理2'
                            },
                            {
                                name: '角色管理3'
                            },
                            {
                                name: '菜單管理4'
                            }
                        ]
                    }
                ]
            }
        },
        methods:{
            toggle(item) {
                    if(!item.open){
                            this.menus.map((v)=>{
                                v.open=false
                        })
                    }
                    item.open=!item.open
            },
            abc(index){
                this.i=0
                this.i = index
            }, 
        }
   

    })
</script>
</body>  
</html>  
回答
編輯回答
晚風(fēng)眠

在toggle方法后面加this.i = null

如果你還想再次點(diǎn)擊之前點(diǎn)擊的導(dǎo)航,其子級active不變,就在menus下面為每一個一級導(dǎo)航添加activeIndex變量用于記錄子級導(dǎo)航的激活下標(biāo)。
這樣點(diǎn)擊了權(quán)限管理下的角色管理1,然后點(diǎn)擊電池管理下的用戶管理2,再次回到權(quán)限管理角色管理1也是激活狀態(tài)

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8">  
    <title>vue點(diǎn)擊切換顯示隱藏</title>  
    <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>  
    <style type="text/css">  
    *{  
        padding: 0;  
        margin: 0;  
        font-size: 14px;  
    }  
    ul{  
        width: 200px;  
        height: auto;  
    }  
    h2{  
        background: green;  
        border: 1px solid #fff;  
        color: #fff;  
        height: 30px;  
        line-height: 30px;  
        text-indent: 24px;  
    }  
    h3{  
        background: #999;  
        height: 24px;  
        line-height: 24px;  
        border: 1px solid #fff;  
        text-indent: 50px;  
    }  
    a{
        display: block;
        width: 200px;
    }
    .zhe li.actives{
        color: #fff;
        background: red;
    }
    </style>  
</head>  
<body>  
    <div id="app">
    <ul>
        <li v-for="(menu,menuindex) in menus" style="background:#ccc;margin:10px 0">
            <a  @click="toggle(menu)">{{menu.name}}</a>
            <ul v-show="menu.open" style="background:green;" class="zhe">
                <li v-for="(item,index) in menu.list" @click = "abc(index,menuindex)" :class="{'actives':menu.activeIndex==index}">{{item.name}}</li>
            </ul>
        </li>
    </ul>
</div>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                     i:null,
                menus: [
                    {
                        name: '權(quán)限管理',
                        open: false,
                        id:'aaa',
                        activeIndex:null,
                        list: [
                            {
                                name: '用戶管理1'
                            },
                            {
                                name: '部門管理1'
                            },
                            {
                                name: '角色管理1'
                            },
                            {
                                name: '菜單管理1'
                            }
                        ]
                    },
                    {
                        name: '電池管理',
                        open: false,
                        id:'bbb',
                        activeIndex:null,
                        list: [
                            {
                                name: '用戶管理2'
                            },
                            {
                                name: '部門管理2'
                            },
                            {
                                name: '角色管理3'
                            },
                            {
                                name: '菜單管理4'
                            }
                        ]
                    }
                ]
            }
        },
        methods:{
            toggle(item) {
                    if(!item.open){
                            this.menus.map((v)=>{
                                v.open=false
                        })
                    }
                    item.open=!item.open
            },
            abc(index,menuindex){
                this.menus[menuindex].activeIndex = index
            }, 
        }
   

    })
</script>
</body>  
</html>  
2018年7月1日 00:29
編輯回答
巷尾

我就修改了一下toggle方法,你那個需求應(yīng)該是能實現(xiàn),但具體的用戶體驗?zāi)憧梢远嘧聊プ聊?,?yōu)化一下:

toggle(item) {
    if(!item.open){ //點(diǎn)擊其它未展開的標(biāo)簽時
        this.menus.map((v)=>{
            v.open=false
        });
        this.i = null;  //讓i重置為null或者小于0的數(shù)也可以
    }
    item.open=!item.open
}
2017年11月28日 22:58
編輯回答
安淺陌

是因為你下面menu.list共用的標(biāo)簽是一樣 它只是改變了值 并沒有改變標(biāo)簽 以及樣式

所以上面操作的第幾個標(biāo)簽 下面第幾個標(biāo)簽打開也會有樣式

恢復(fù)初始值

clipboard.png

2018年6月7日 16:15
編輯回答
懷中人

比如你點(diǎn)第一個菜單的第一個 那index是0把, 那this.i 變0了把。 那你第二個菜單的第一個選項的條件也符合了把?

2017年10月15日 22:04
編輯回答
不舍棄

重置i 的值 是可以的

2017年1月13日 12:48
編輯回答
舊螢火

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>vue點(diǎn)擊切換顯示隱藏</title>
    <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    <style type="text/css">
        *{
            padding: 0;
            margin: 0;
            font-size: 14px;
        }
        ul{
            width: 200px;
            height: auto;
        }
        h2{
            background: green;
            border: 1px solid #fff;
            color: #fff;
            height: 30px;
            line-height: 30px;
            text-indent: 24px;
        }
        h3{
            background: #999;
            height: 24px;
            line-height: 24px;
            border: 1px solid #fff;
            text-indent: 50px;
        }
        a{
            display: block;
            width: 200px;
        }
        .zhe li.actives{
            color: #fff;
            background: red;
        }
    </style>
</head>
<body>
<div id="app">
    <ul>
        <li v-for="(menu,idx) in menus" style="background:#ccc;margin:10px 0">
            <a  @click="toggle(menu)">{{menu.name}}{{idx}}</a>
            <ul v-show="menu.open" style="background:green;" class="zhe">
                <li v-for="(item,index) in menu.list" @click = "abc(idx,index)" :class="{'actives':menu.i==index}">{{item.name}}</li>
            </ul>
        </li>
    </ul>
</div>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                menus: [
                    {
                        name: '權(quán)限管理',
                        open: false,
                        id:'aaa',
                        i:null,
                        list: [
                            {
                                name: '用戶管理1'
                            },
                            {
                                name: '部門管理1'
                            },
                            {
                                name: '角色管理1'
                            },
                            {
                                name: '菜單管理1'
                            }
                        ]
                    },
                    {
                        name: '電池管理',
                        open: false,
                        id:'bbb',
                        i:null,
                        list: [
                            {
                                name: '用戶管理2'
                            },
                            {
                                name: '部門管理2'
                            },
                            {
                                name: '角色管理3'
                            },
                            {
                                name: '菜單管理4'
                            }
                        ]
                    }
                ]
            }
        },
        methods:{
            toggle(item) {
                if(!item.open){
                    this.menus.map((v)=>{
                        v.open=false;
                        v.i=null
                    })
                }
                item.open=!item.open
            },
            abc(idx,index){
                this.menus[idx].i = index
            },
        }
    })
</script>
</body>
</html>
2018年2月17日 13:52
編輯回答
茍活
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>vue點(diǎn)擊切換顯示隱藏</title>
  <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  <style type="text/css">
  * {
    padding: 0;
    margin: 0;
    font-size: 14px;
  }

  ul {
    width: 200px;
    height: auto;
  }

  h2 {
    background: green;
    border: 1px solid #fff;
    color: #fff;
    height: 30px;
    line-height: 30px;
    text-indent: 24px;
  }

  h3 {
    background: #999;
    height: 24px;
    line-height: 24px;
    border: 1px solid #fff;
    text-indent: 50px;
  }

  a {
    display: block;
    width: 200px;
  }

  .zhe li.actives {
    color: #fff;
    background: red;
  }
  </style>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="(menu, idx) in menus" style="background:#ccc;margin:10px 0">
        <a @click="toggle(menu)">{{menu.name}}</a>
        <ul v-show="menu.open" style="background:green;" class="zhe">
          <li v-for="(item,index) in menu.list" @click="abc(idx, item)" :class="item.isActive ? 'actives' : ''">{{item.name}}</li>
        </ul>
      </li>
    </ul>
  </div>
  <script>
  new Vue({
    el: "#app",
    data() {
      return {
        i: null,
        menus: [{
            name: '權(quán)限管理',
            open: false,
            id: 'aaa',
            list: [{
                name: '用戶管理1',
                isActive: false
              },
              {
                name: '部門管理1',
                isActive: false
              },
              {
                name: '角色管理1',
                isActive: false
              },
              {
                name: '菜單管理1',
                isActive: false
              }
            ]
          },
          {
            name: '電池管理',
            open: false,
            id: 'bbb',
            list: [{
                name: '用戶管理2',
                isActive: false
              },
              {
                name: '部門管理2',
                isActive: false
              },
              {
                name: '角色管理3',
                isActive: false
              },
              {
                name: '菜單管理4',
                isActive: false
              }
            ]
          }
        ]
      }
    },
    methods: {
      toggle(item) {
        var index = this.menus.indexOf(item)
        if (!item.open) {
          this.menus.map((v, i) => {
            if (i !== index) {
              v.open = false
              v.list.map(_o => {
                _o.isActive = false
              })
            }
          })
        }
        item.open = !item.open
      },
      abc(idx, item) {
        this.menus[idx]['list'].forEach((o, i) => {
          o.isActive = false
        })
        item.isActive = true
      },
    }


  })
  </script>
</body>

</html>

不知道是不是你想要的結(jié)果, 希望幫助到你

2018年3月24日 23:18