Vue.js 单击并删除前一个时添加活动的类

新手上路,请多包涵

我想在我的 div 元素上实现这个活动链接在这里你可以看到我想用我的代码做的例子

http://jsfiddle.net/fiddleyetu/9ff79/

 $(function() {
  $( 'ul.nav li' ).on( 'click', function() {
        $( this ).parent().find( 'li.active' ).removeClass( 'active' );
        $( this ).addClass( 'active' );
  });
});

在这里使用 vue.js 我不能在我的 div 元素上做活动链接

图 1

在此处输入图像描述

这是我必须在其上激活链接的元素的代码

    <div class="conversion">
    <div class="file has-text-centered icon-active-color" v-on:click="activeiconc">
        <img src="../imgs/png.png"/>
        <h4>.PNG</h4>
    </div>
    <div class="file has-text-centered" v-on:click="activeicon" v-bind:class="{ 'icon-active-color':activeiconc,}">
        <img src="../imgs/pdf.png"/>
        <h4>.PDF</h4>
    </div>
    <div class="file has-text-centered" v-on:click="activeicon" v-bind:class="{ 'icon-active-color':activeiconc }">
        <img src="../imgs/jpg.png"/>
        <h4>.JPG</h4>
    </div>
    <div class="file has-text-centered" v-on:click="activeicon" v-bind:class="{ 'icon-active-color':activeiconc }">
        <img src="../imgs/psd.png"/>
        <h4>.PSD</h4>
    </div>
</div>

js

  export default {
components: {
  MainLayout
},
    data: function(){
    return {
      logo: false,
      color:false,
      list:true,
      grid:false,
      deletebtn:false,
      isImageModalActive: false,
      activerow: false,
      activeiconc:false,
    }
  },
  methods:{

    showgrid:function(){
        this.grid = true;
        this.list = false;

    },
    showlist:function(){
        this.list ^=true;
        this.grid = false
    },
    showLogo:function(){
        this.logo ^= true;
        this.color = false;
        this.deletebtn ^= true;
        this.activerow ^= true
    },
    showColor:function(){
        this.color ^= true;
        this.logo = false;
    },
    activeicon:function(){
        this.activeiconc ^= true;
    }
  }
}

原文由 Haroon Aslam 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 363
2 个回答

我是 Vue 的新手,但是将 JQuery 示例转换为 Vue.js 的一种简单方法是: Jsfiddle demo

基本上,您需要将活动元素存储在 Vue 数据中,并根据 in 设置类。您可以使用 v-for 来呈现列表。

HTML 部分:

 <div id="app">
  <ul>
    <li @click="activate(1)" :class="{ active : active_el == 1 }">Link 1</li>
    <li @click="activate(2)" :class="{ active : active_el == 2 }">Link 2</li>
    <li @click="activate(3)" :class="{ active : active_el == 3 }">Link 3</li>
  </ul>
</div>

Vue.js

 var app = new Vue({
  el:"#app",
  data:{
    active_el:0
  },
  methods:{
    activate:function(el){
        this.active_el = el;
    }
  }
});

原文由 verjas 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果你想直接通过代码激活,VueJS允许你将锚标签的active直接绑定到li元素的索引上,这样当索引绑定的vuejs变量发生变化时,active也会发生变化。检查这两个链接以获取更多详细信息

这是解决方案的关键

:class="{current:i == current}

在下面的小提琴和另一篇文章中可用,以故事格式解释如何在 vuejs 中动态控制锚活动

https://jsfiddle.net/Herteby/kpkcfcdw/

https://stackoverblow.wordpress.com/2021/04/03/how-modern-javascript-makes-click-simulation/

原文由 veritas 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题